Improve error handling when the client or server quits