Added code to automatically disconnect if the sockets are no longer 
valid