arrume isso #include <iostream>
#include <map>
#include <string>
#include <sstream>
#include <cstdlib>
#include <winsock2.h>
#include <windows.h>

// Função de conversão para string
template <typename T>
std::string to_string(T value) {
    std::ostringstream oss;
    oss << value;
    return oss.str();
}

// Classe para gerenciar JSON
class JSONManager {
public:
    // Estrutura para representar um valor JSON
    struct JSONValue {
        enum Type { StringType, IntType, DoubleType };
        Type type;
        std::string value;

        JSONValue() : type(StringType), value("") {}

        JSONValue(Type t, const std::string& v) : type(t), value(v) {}
        JSONValue(int v) : type(IntType) { value = intToString(v); }
        JSONValue(double v) : type(DoubleType) { value = doubleToString(v); }
        JSONValue(const std::string& v) : type(StringType), value(v) {}

        std::string intToString(int n) {
            std::stringstream ss;
            ss << n;
            return ss.str();
        }

        std::string doubleToString(double d) {
            std::stringstream ss;
            ss << d;
            return ss.str();
        }
    };

    // Método para serializar um objeto JSON para string
    static std::string serialize(const std::map<std::string, JSONValue>& jsonObject) {
        std::string result = "{";
        std::map<std::string, JSONValue>::const_iterator it = jsonObject.begin();
        while (it != jsonObject.end()) {
            result += "\"" + it->first + "\":" + "\"" + valueToString(it->second) + "\"";
            if (++it != jsonObject.end()) {
                result += ",";
            }
        }
        result += "}";
        return result;
    }

    // Método para desserializar uma string JSON para um objeto JSON
    static std::map<std::string, JSONValue> deserialize(const std::string& jsonString) {
        std::map<std::string, JSONValue> result;

        size_t startPos = jsonString.find("{") + 1;
        size_t endPos = jsonString.rfind("}");
        std::string jsonContent = jsonString.substr(startPos, endPos - startPos);

        size_t pos = 0;
        while (pos < jsonContent.size()) {
            size_t colonPos = jsonContent.find(":", pos);
            size_t commaPos = jsonContent.find(",", pos);

            std::string key = jsonContent.substr(pos, colonPos - pos);
            key = removeQuotes(key);

            std::string value;
            if (commaPos != std::string::npos) {
                value = jsonContent.substr(colonPos + 1, commaPos - colonPos - 1);
            } else {
                value = jsonContent.substr(colonPos + 1);
            }

            JSONValue typedValue = convertFromString(value);
            result.insert(std::make_pair(key, typedValue));

            if (commaPos != std::string::npos) {
                pos = commaPos + 1;
            } else {
                break;
            }
        }

        return result;
    }

    // Método para modificar o valor de uma chave em um objeto JSON
    static bool modifyValue(std::map<std::string, JSONValue>& jsonObject, const std::string& key, const JSONValue& newValue) {
        std::map<std::string, JSONValue>::iterator it = jsonObject.find(key);
        if (it != jsonObject.end()) {
            it->second = newValue;
            return true;
        } else {
            return false;
        }
    }

private:
    // Método auxiliar para converter um valor JSON para string
    static std::string valueToString(const JSONValue& value) {
        return value.value;
    }

    // Método auxiliar para remover as aspas de uma string
    static std::string removeQuotes(const std::string& input) {
        std::string result = input;
        result.erase(std::remove(result.begin(), result.end(), '\"'), result.end());
        return result;
    }

    // Método auxiliar para converter uma string para um valor JSON
    static JSONValue convertFromString(const std::string& value) {
        if (isString(value)) {
            return JSONValue(JSONValue::StringType, removeQuotes(value));
        } else if (isInteger(value)) {
            return JSONValue(atoi(value.c_str()));
        } else if (isDouble(value)) {
            return JSONValue(atof(value.c_str()));
        } else {
            return JSONValue();
        }
    }

    // Método auxiliar para verificar se uma string representa um valor string JSON
    static bool isString(const std::string& value) {
        return (!value.empty() && value[0] == '\"' && value[value.length() - 1] == '\"');
    }

    // Método auxiliar para verificar se uma string representa um valor inteiro JSON
    static bool isInteger(const std::string& value) {
        size_t pos;
        std::istringstream iss(value);
        int testInt;
        iss >> std::noskipws >> testInt;
        return (!iss.fail() && iss.eof());
    }

    // Método auxiliar para verificar se uma string representa um valor double JSON
    static bool isDouble(const std::string& value) {
        size_t pos;
        std::istringstream iss(value);
        double testDouble;
        iss >> std::noskipws >> testDouble;
        return (!iss.fail() && iss.eof());
    }
};

class WebSocketClient {
public:
    WebSocketClient(const std::string& serverAddress, int serverPort, const std::string& path)
        : serverAddress(serverAddress), serverPort(serverPort), path(path), clientSocket(INVALID_SOCKET), communicationThread(0) {}

    ~WebSocketClient() {
        StopThread();
        CloseConnection();
        CleanupWinsock();
    }

    void Connect() {
        if (!InitializeWinsock()) {
            return;
        }

        if (!CreateSocket()) {
            CleanupWinsock();
            return;
        }

        if (!EstablishConnection()) {
            CleanupWinsock();
            return;
        }

        std::cout << "Connected to the WebSocket server." << std::endl;

        if (!WebSocketHandshake()) {
            std::cerr << "WebSocket handshake failed." << std::endl;
            CleanupWinsock();
            return;
        }

        std::map<std::string, JSONManager::JSONValue> jsonObject;
        jsonObject["comando"] = JSONManager::JSONValue(1);
        jsonObject["nomeJogo"] = JSONManager::JSONValue("Nine");
        jsonObject["terminal"] = JSONManager::JSONValue(1);
        jsonObject["aposta"] = JSONManager::JSONValue(2);
        jsonObject["valorMoeda"] = JSONManager::JSONValue(20);
        jsonObject["upAc1"] = JSONManager::JSONValue(0.1);
        jsonObject["upAc1X"] = JSONManager::JSONValue(0);
        jsonObject["upCxRemoto"] = JSONManager::JSONValue(1.2);
        jsonObject["Grupo"] = JSONManager::JSONValue(1);

        SendWebSocketHandshake();

        communicationThread = CreateThread(NULL, 0, &WebSocketClient::WebSocketCommunicationThreadStatic, this, 0, NULL);
    }

    void StopThread() {
        if (communicationThread != 0) {
            WaitForSingleObject(communicationThread, INFINITE);
            CloseHandle(communicationThread);
            communicationThread = 0;
        }
    }

    void SendWebSocketHandshake() {
        const std::string CRLF = "\r\n";
        const std::string key = "x3JJHMbDL1EzLkh9GBhXDw==";

        std::string handshakeRequest =
            "GET " + path + " HTTP/1.1" + CRLF +
            "Host: " + serverAddress + ":" + to_string(serverPort) + CRLF +
            "Connection: Upgrade" + CRLF +
            "Upgrade: websocket" + CRLF +
            "Sec-WebSocket-Key: " + key + CRLF +
            "Sec-WebSocket-Version: 13" + CRLF +
            CRLF;

        send(clientSocket, handshakeRequest.c_str(), handshakeRequest.length(), 0);
    }

private:
    std::string serverAddress;
    int serverPort;
    std::string path;
    SOCKET clientSocket;
    HANDLE communicationThread;

    bool InitializeWinsock() {
        WSADATA wsaData;
        return WSAStartup(MAKEWORD(2, 2), &wsaData) == 0;
    }

    void CleanupWinsock() {
        WSACleanup();
    }

    bool CreateSocket() {
        clientSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
        if (clientSocket == INVALID_SOCKET) {
            std::cerr << "Error creating socket: " << WSAGetLastError() << std::endl;
            return false;
        }
        return true;
    }

    bool EstablishConnection() {
        sockaddr_in serverAddr;
        serverAddr.sin_family = AF_INET;
        serverAddr.sin_port = htons(serverPort);
        serverAddr.sin_addr.s_addr = inet_addr(serverAddress.c_str());

        if (connect(clientSocket, (sockaddr*)&serverAddr, sizeof(serverAddr)) == SOCKET_ERROR) {
            std::cerr << "Error connecting to server: " << WSAGetLastError() << std::endl;
            return false;
        }
        return true;
    }

    void CloseConnection() {
        closesocket(clientSocket);
    }

    bool WebSocketHandshake() {
        const int bufferSize = 4096;
        char buffer[bufferSize];

        int bytesRead = recv(clientSocket, buffer, bufferSize, 0);
        if (bytesRead == SOCKET_ERROR) {
            std::cerr << "Error receiving handshake response: " << WSAGetLastError() << std::endl;
            return false;
        }

        buffer[bytesRead] = '\0';
        std::string response(buffer);

        if (response.find("HTTP/1.1 101 Switching Protocols") == std::string::npos) {
            std::cerr << "Invalid handshake response." << std::endl;
            return false;
        }

        return true;
    }

    static DWORD WINAPI WebSocketCommunicationThreadStatic(LPVOID param) {
        WebSocketClient* webSocketClient = static_cast<WebSocketClient*>(param);
        webSocketClient->WebSocketCommunicationThread();
        return 0;
    }

    void WebSocketCommunicationThread() {
        const int bufferSize = 4096;
        char buffer[bufferSize];

        while (true) {
            int bytesRead = recv(clientSocket, buffer, bufferSize, 0);
            if (bytesRead == SOCKET_ERROR) {
                std::cerr << "Error receiving data: " << WSAGetLastError() << std::endl;
                break;
            } else if (bytesRead == 0) {
                std::cerr << "Connection closed by peer." << std::endl;
                break;
            }

            ProcessReceivedMessage(buffer, bytesRead);
        }
    }

    void ProcessReceivedMessage(const char* buffer, int length) {
        // Implemente a lógica para processar a mensagem recebida aqui
        // Este é o local onde você lidaria com os dados recebidos do servidor WebSocket
    }
};

int main() {
    try {
        WebSocketClient webSocketClient("192.168.1.200", 12345, "/api");
        webSocketClient.Connect();

        while (true) {
            // Lógica principal do jogo
        }
    } catch (const std::exception& e) {
        std::cerr << "Exception: " << e.what() << std::endl;
    }

    return 0;
}