Fixed several bugs in the server thread.