Implemented most of the server code for client connections.