Correctly handling closing for WebSockets connections