Added code to prevent most of the errors when the client closes the connection