Closing server connection when appropriate