Ensure that the source of the event is the WebSocket.
Fix bug to ensure that when the remote side closes the WebSocket, the state is reset correctly.