Cleans up and refactor the client socket connection code