//Ricevuto un comando di richiesta inserimento al party da parte di un client
        private void ReceivedRequestToParty(int _receiverConnectionID, PartyCommand receivedPartyCommand)
        {
            if (!_partyIDs.Exists(id => id == _receiverConnectionID))
            {
                _partyIDs.Add(_receiverConnectionID);
            }

            int index = _partyIDs.IndexOf(_receiverConnectionID);

            //Save ip
            if (!_partyIPs.Exists(ip => ip == receivedPartyCommand.ipSender))
            {
                _partyIPs.Add(receivedPartyCommand.ipSender);
            }
            else
            {
                if (_partyIPs[index] != receivedPartyCommand.ipSender)
                {
                    _partyIPs[index] = receivedPartyCommand.ipSender;
                }
            }

            //Send Message of request accepted
            PartyCommand partyCommand = new PartyCommand(_myIp, _connectionID, _receiverConnectionID, false);

            SendCommand(partyCommand);
        }
        //Invio al host, dopo avvenuta connessione, di richiesta inserimento al party
        private void Send_Request_ToParty(int hostID, int receiverConnectionID)
        {
            _hostID            = hostID;
            _host_ConnectionID = receiverConnectionID;
            PartyCommand partyCommand = new PartyCommand(_myIp, _connectionID, _host_ConnectionID, true);

            SendCommand(partyCommand);
        }
        private void Client_CheckForNetworkEvents()
        {
            if (!_isStarted)
            {
                return;
            }

            int  recHostId;
            int  connectionId;
            int  channelId;
            int  dataSize;
            int  bufferSize = 1024;
            byte error;

            byte[] recBuffer = new byte[1024];

            NetworkEventType recData = NetworkTransport.Receive(out recHostId, out connectionId, out channelId, recBuffer, bufferSize, out dataSize, out error);

            switch (recData)
            {
            case NetworkEventType.Nothing:
            {
                break;
            }

            case NetworkEventType.ConnectEvent:
            {
                Send_Request_ToParty(recHostId, connectionId);
                _hostID = recHostId;
                _status = Status.Connected;
                break;
            }

            case NetworkEventType.DataEvent:
            {
                CommandType commandType;
                object      o = DecodeJsonCommand(recBuffer, out commandType);
                _lastCommandReceived = commandType;
                switch (commandType)
                {
                case CommandType.Party:
                {
                    PartyCommand partyCommand = (PartyCommand)o;
                    commandsReceived++;
                    _status = Status.Partying;
                    break;
                }

                case CommandType.Test:
                    commandsReceived++;
                    break;

                case CommandType.None:
                    commandsReceived++;
                    break;
                }

                OnReceived((BaseCommand)o);
                break;
            }

            case NetworkEventType.DisconnectEvent:
            {
                _lastMessageError = "" + (NetworkError)error;
                _status           = Status.Disconnected;
                StartClienting(hostIP, port);
                break;
            }
            }
        }
        //Chiamato in Update, una volta inizializzato come server, rimane in ascolto di eventuali eventi.
        //In relazione all'evento vengono effettuate le funzioni relative.
        private void Host_CheckForNetworkEvents()
        {
            if (!_isStarted)
            {
                return;
            }

            int  recHostId;
            int  connectionId;
            int  channelId;
            int  dataSize;
            int  bufferSize = 1024;
            byte error;

            byte[] recBuffer = new byte[1024];

            NetworkEventType recData = NetworkTransport.Receive(out recHostId, out connectionId, out channelId, recBuffer, bufferSize, out dataSize, out error);

            switch (recData)
            {
            case NetworkEventType.Nothing:
            {
                break;
            }

            case NetworkEventType.ConnectEvent:
            {
                break;
            }

            case NetworkEventType.DataEvent:
            {
                CommandType commandType;
                object      o = DecodeJsonCommand(recBuffer, out commandType);
                _lastCommandReceived = commandType;

                switch (commandType)
                {
                case CommandType.Party:
                {
                    PartyCommand partyCommand = (PartyCommand)o;
                    if (partyCommand.isRequest)
                    {
                        ReceivedRequestToParty(connectionId, partyCommand);
                    }
                    commandsReceived++;
                    break;
                }

                case CommandType.Test:
                {
                    commandsReceived++;
                    break;
                }

                case CommandType.None:
                {
                    commandsReceived++;
                    break;
                }
                }

                //Catch exception
                OnReceived((BaseCommand)o);
                break;
            }

            case NetworkEventType.DisconnectEvent:
            {
                break;
            }
            }
        }