Improved WebSocket handler