Fix to the connection logic - socket.isConnected() always returns true after a connection has been made, even if the socket is later closed.