    if (socket) {
                socket.close();
                socket = null;
            }
        }

        function sendMessage(message, isJson = false) {
            if (!socket || socket.readyState !== WebSocket.OPEN) {
                addMessage('error', 'Not connected! Please connect first.');
                return false;
            }

            socket.send(message);
            messagesSent++;
            updateStats();
            addMessage('sent', message, isJson);
            return true