public void MakeMove(int MoveNum)
    {
        MoveTakenMsg m = new MoveTakenMsg();

        m.move  = MoveNum;
        m.Lobby = MyLobby;
        SendToServer(JsonUtility.ToJson(m));
    }
    void OnData(DataStreamReader stream, int i)  // i is the index in m_connection to get ip address
    {
        NativeArray <byte> bytes = new NativeArray <byte>(stream.Length, Allocator.Temp);

        stream.ReadBytes(bytes);
        string        recMsg = Encoding.ASCII.GetString(bytes.ToArray());
        NetworkHeader header = JsonUtility.FromJson <NetworkHeader>(recMsg);

        switch (header.cmd)
        {
        case Commands.PLAYER_LOGIN:
            Debug.Log("Login request received");
            PlayerLoginMsg loginMsg = JsonUtility.FromJson <PlayerLoginMsg>(recMsg);
            StartCoroutine(SendLoginWebRequest(loginMsg.userID, loginMsg.password, i));
            break;

        case Commands.PLAYER_REGISTER:
            Debug.Log("Registration request received");
            PlayerRegisterMsg registerMsg = JsonUtility.FromJson <PlayerRegisterMsg>(recMsg);
            StartCoroutine(SendRegisterWebRequest(registerMsg.userID, registerMsg.password, i));
            break;

        case Commands.HOST_GAME:
            Debug.Log("HOSTED GAME received");
            HostGameMsg hostMsg = JsonUtility.FromJson <HostGameMsg>(recMsg);
            HostNewLobby(hostMsg.player.id, i);
            break;

        case Commands.JOIN_GAME:
            Debug.Log("JOINED GAME received");
            JoinGameMsg joinMsg = JsonUtility.FromJson <JoinGameMsg>(recMsg);
            JoinLobby(joinMsg.joinLobby.lobbyID, joinMsg.player.id, i);

            break;

        case Commands.HANDSHAKE:
            HandshakeMsg hsMsg = JsonUtility.FromJson <HandshakeMsg>(recMsg);
            SendToClient(JsonUtility.ToJson(hsMsg), m_Connections[i]);
            //Debug.Log("Handshake message received!");
            break;

        case Commands.PLAYER_UPDATE:
            PlayerUpdateMsg puMsg = JsonUtility.FromJson <PlayerUpdateMsg>(recMsg);
            //Debug.Log("Player update message received!");
            //update the specific players data
            foreach (NetworkObjects.NetworkPlayer player in connectedPlayers)
            {
            }
            break;

        case Commands.START_GAME:
            StartGameMsg startMsg = JsonUtility.FromJson <StartGameMsg>(recMsg);
            startMsg.successful  = true;
            startMsg.Player1Char = UnityEngine.Random.Range(0, 4);
            int player2 = UnityEngine.Random.Range(0, 4);
            while (startMsg.Player1Char == player2)
            {
                player2 = UnityEngine.Random.Range(0, 4);
            }
            startMsg.Player2Char = player2;
            Debug.Log("Start Message Sucess:");
            Debug.Log(startMsg.successful ? "Sucess": "Fail");
            Debug.Log(startMsg);
            SendToClient(JsonUtility.ToJson(startMsg), m_Connections[i]);
            SendToClient(JsonUtility.ToJson(startMsg), m_Connections[startMsg.LobbyToStart.player2addr]);

            break;

        case Commands.SERVER_UPDATE:
            ServerUpdateMsg suMsg = JsonUtility.FromJson <ServerUpdateMsg>(recMsg);
            Debug.Log("Server update message received!");
            break;

        case Commands.REQUEST_AVAILABLE_LOBBIES:
            Debug.Log("Received request for lobbies");
            AllAvailableLobbies n = new AllAvailableLobbies();
            foreach (KeyValuePair <int, NetworkObjects.Lobby> Lobby in AvailableLobbies)
            {
                if (!Lobby.Value.full)
                {
                    n.Lobbies.Add(Lobby.Value);
                }
            }
            //n.Lobbies = AvailableLobbies;
            //Debug.Log(JsonUtility.ToJson(AvailableLobbies[0]));
            //Debug.Log(JsonUtility.ToJson(n.Lobbies[0]));
            SendToClient(JsonUtility.ToJson(n), m_Connections[i]);
            break;

        case Commands.MOVE_TAKEN:
            MoveTakenMsg moveMsg = JsonUtility.FromJson <MoveTakenMsg>(recMsg);
            Debug.Log("Received Move from client");
            if (moveMsg.Lobby.player1addr == i)    //if player 1 made the move
            {
                SendToClient(JsonUtility.ToJson(moveMsg), m_Connections[moveMsg.Lobby.player2addr]);
            }
            else
            {
                SendToClient(JsonUtility.ToJson(moveMsg), m_Connections[moveMsg.Lobby.player1addr]);
            }
            break;

        case Commands.PLAYER_INFO:
            MyInfoMsg infoMsg = JsonUtility.FromJson <MyInfoMsg>(recMsg);
            Debug.Log("Received Player Info from " + infoMsg.Player.UserID);
            StartCoroutine(PlayerInfoWebRequest(infoMsg.Player.UserID, i));
            break;

        case Commands.BATTLE_WON:
            BattleWinMsg winMsg = JsonUtility.FromJson <BattleWinMsg>(recMsg);
            Debug.Log("Received Move from client");
            if (winMsg.Lobby.player1addr == i)    //if player 1 won
            {
                StartCoroutine(SendWinAndLossWebRequest(winMsg.Lobby.Player1, winMsg.Lobby.Player2));
                //update player 1 win and player 2 loss
            }
            else
            {
                StartCoroutine(SendWinAndLossWebRequest(winMsg.Lobby.Player2, winMsg.Lobby.Player1));
                //update player 2 win and player 1 loss
            }
            //remove the lobby from the available lobbies list
            AvailableLobbies.Remove(winMsg.Lobby.lobbyID);
            break;

        default:
            Debug.Log("SERVER ERROR: Unrecognized message received!");
            break;
        }
    }
    void OnData(DataStreamReader stream)
    {
        NativeArray <byte> bytes = new NativeArray <byte>(stream.Length, Allocator.Temp);

        stream.ReadBytes(bytes);
        string        recMsg = Encoding.ASCII.GetString(bytes.ToArray());
        NetworkHeader header = JsonUtility.FromJson <NetworkHeader>(recMsg);

        switch (header.cmd)
        {
        case Commands.PLAYER_LOGIN:
            PlayerLoginMsg loginMsg = JsonUtility.FromJson <PlayerLoginMsg>(recMsg);
            // check if successful is true
            if (loginMsg.successful)
            {
                Debug.Log("Successful Login");
                PlayerUserID = loginMsg.userID;
                //RequestPlayerInfo();
                SceneManager.LoadScene("Lobbies");
            }
            else
            {
                Debug.Log("UNSuccessful Login");
                FindObjectOfType <LoginButtonBehaviour>().DisplayError();
            }
            break;

        case Commands.PLAYER_REGISTER:
            PlayerRegisterMsg registerMsg = JsonUtility.FromJson <PlayerRegisterMsg>(recMsg);
            if (registerMsg.successful)
            {
                Debug.Log("Successful Register");
                PlayerUserID = registerMsg.userID;
                //RequestPlayerInfo();
                SceneManager.LoadScene("Lobbies");
            }
            else
            {
                Debug.Log("UNSuccessful Register");
                FindObjectOfType <RegisterButtonBehaviour>().DisplayError();
            }
            // check if successful is true
            break;

        case Commands.HOST_GAME:
            HostGameMsg hostmsg = JsonUtility.FromJson <HostGameMsg>(recMsg);
            if (hostmsg.successful && (hostmsg.newLobby.Player1 == PlayerUserID || hostmsg.newLobby.Player2 == PlayerUserID))
            {
                MyLobby = hostmsg.newLobby;
                SceneManager.LoadScene("Lobby");
                // success move to the lobby scene
            }
            else
            {
                // display error
            }
            break;

        case Commands.JOIN_GAME:
            JoinGameMsg joinmsg = JsonUtility.FromJson <JoinGameMsg>(recMsg);
            if (joinmsg.successful && (joinmsg.joinLobby.Player1 == PlayerUserID || joinmsg.joinLobby.Player2 == PlayerUserID))
            {
                MyLobby = joinmsg.joinLobby;
                if (joinmsg.joinLobby.Player1 == PlayerUserID)
                {
                    MyLevel       = joinmsg.joinLobby.HostWins;
                    OpponentLevel = joinmsg.joinLobby.Player2Wins;
                    FindObjectOfType <LobbyHandler>().UpdateLobby();
                }
                else
                {
                    MyLevel       = joinmsg.joinLobby.Player2Wins;
                    OpponentLevel = joinmsg.joinLobby.HostWins;
                    SceneManager.LoadScene("Lobby");
                }

                // success move to the lobby scene
            }
            else
            {
                // display error
            }
            break;

        case Commands.HANDSHAKE:
            HandshakeMsg hsMsg = JsonUtility.FromJson <HandshakeMsg>(recMsg);

            Debug.Log("Handshake message received!");
            //myServerId = hsMsg.InternalServerID;
            //Debug.Log("My id is:" + myServerId);
            ////add our own id so we know who we are
            //if (myId == "")
            //{
            //    myId = hsMsg.player.id;
            //    Debug.Log("My id is:" + myId);
            //}
            break;

        case Commands.START_GAME:
            StartGameMsg startMsg = JsonUtility.FromJson <StartGameMsg>(recMsg);
            if (startMsg.LobbyToStart.Player1 == PlayerUserID)    // i am player 1
            {
                MyPlayerCharacter    = startMsg.Player1Char;
                EnemyPlayerCharacter = startMsg.Player2Char;
            }
            else     // i am player 2
            {
                MyPlayerCharacter    = startMsg.Player2Char;
                EnemyPlayerCharacter = startMsg.Player1Char;
            }
            if (startMsg.successful)
            {
                EnterPlay();
            }
            break;

        case Commands.PLAYER_UPDATE:
            //not really receiveing player update messages as this is this client and not the server
            PlayerUpdateMsg puMsg = JsonUtility.FromJson <PlayerUpdateMsg>(recMsg);
            Debug.Log("Player update message received!");
            break;

        case Commands.SERVER_UPDATE:
            ServerUpdateMsg suMsg = JsonUtility.FromJson <ServerUpdateMsg>(recMsg);
            Debug.Log("Server update message received!");
            for (int i = 0; i < suMsg.players.Count; i++)
            {
            }
            break;

        case Commands.REQUEST_ALL_LOBBIES:
            AllAvailableLobbies alMsg = JsonUtility.FromJson <AllAvailableLobbies>(recMsg);
            Debug.Log("Server update message received!");
            for (int i = 0; i < alMsg.Lobbies.Count; i++)
            {
                Debug.Log(JsonUtility.ToJson(alMsg.Lobbies[i]));
            }
            ScrollFiller scrollFiller = FindObjectOfType <ScrollFiller>();
            scrollFiller.ClearLobbies();
            foreach (var lobby in alMsg.Lobbies)
            {
                //if (!lobby.full)
                scrollFiller.GenerateItem(lobby, lobby.HostWins);
            }
            break;

        case Commands.MOVE_TAKEN:
            MoveTakenMsg moveMsg = JsonUtility.FromJson <MoveTakenMsg>(recMsg);
            Debug.Log("Received move from player");
            FindObjectOfType <BattleSystem>().EnemyAttack(moveMsg.move);

            break;

        case Commands.PLAYER_INFO:
            MyInfoMsg infoMsg = JsonUtility.FromJson <MyInfoMsg>(recMsg);
            Debug.Log("Received info about player");
            Debug.Log("RAW MESSAGE PLAYER INFO " + recMsg);
            Player = infoMsg.Player;
            break;

        case Commands.LOBBY_DISCONNECTED:
            Debug.Log("Other player disconnected form lobby");
            OnOtherPlayerDisconnected();
            break;

        default:
            Debug.Log("Unrecognized message received!");
            Debug.Log(recMsg);
            break;
        }
    }