handling the socket connection logic