IEnumerator connectToNATFacilitatorTest()
    {
        StartupResult startResult = rakPeerTest.Startup(2, new SocketDescriptor(), 1);

        if (startResult != StartupResult.RAKNET_STARTED)
        {
            Debug.Log("Failed to initialize network interface 2: " + startResult.ToString());
            yield break;
        }
        ConnectionAttemptResult connectResult = rakPeerTest.Connect(facilitatorIP, (ushort)facilitatorPort, null, 0);

        if (connectResult != ConnectionAttemptResult.CONNECTION_ATTEMPT_STARTED)
        {
            Debug.Log("Failed to initialize connection to NAT Facilitator 2: " + connectResult.ToString());
            yield break;
        }

        Packet packet;

        while ((packet = rakPeerTest.Receive()) == null)
        {
            yield return(new WaitForEndOfFrame());
        }

        if (packet.data[0] != (byte)DefaultMessageIDTypes.ID_CONNECTION_REQUEST_ACCEPTED)
        {
            Debug.Log("Failed to connect to NAT Facilitator 2: " + ((DefaultMessageIDTypes)packet.data[0]));
            yield break;
        }

        guidTest = rakPeerTest.GetMyGUID().g.ToString();
        Debug.Log("Connected 2: " + guidTest);

        facilitatorSystemAddressTest = packet.systemAddress;

        externalIP = rakPeerTest.GetExternalID(packet.systemAddress).ToString(false);

        rakPeerTest.SetMaximumIncomingConnections(2);

        if (firstTimeConnectTest)
        {
            firstTimeConnectTest = false;

            natPunchthroughClientTest = new NatPunchthroughClient();
            rakPeerTest.AttachPlugin(natPunchthroughClientTest);

            natPunchthroughClientTest.FindRouterPortStride(facilitatorSystemAddressTest);
        }
        else
        {
            StartCoroutine("waitForIncomingNATPunchThroughOnServerTest");
        }
        connectedToFacTest = true;

        isReadyTest = true;
    }
    /**
     * Connect to the externally hosted NAT Facilitator (NATCompleteServer from the RakNet samples)
     * This is called initially and also after each succesfull punchthrough received on the server.
     */
    IEnumerator connectToNATFacilitator()
    {
        // Start the RakNet interface listening on a random port
        // We never need more than 2 connections, one to the Facilitator and one to either the server or the latest incoming client
        // Each time a client connects on the server RakNet is shut down and restarted with two fresh connections
        StartupResult startResult = rakPeer.Startup(2, new SocketDescriptor(), 1);

        if (startResult != StartupResult.RAKNET_STARTED)
        {
            Debug.Log("Failed to initialize network interface: " + startResult.ToString());
            yield break;
        }

        // Connect to the Facilitator
        ConnectionAttemptResult connectResult = rakPeer.Connect(facilitatorIP, (ushort)facilitatorPort, null, 0);

        if (connectResult != ConnectionAttemptResult.CONNECTION_ATTEMPT_STARTED)
        {
            Debug.Log("Failed to initialize connection to NAT Facilitator: " + connectResult.ToString());
            yield break;
        }

        // Connecting, wait for response
        Packet packet;

        while ((packet = rakPeer.Receive()) == null)
        {
            yield return(new WaitForEndOfFrame());
        }

        // Was the connection accepted?
        if (packet.data[0] != (byte)DefaultMessageIDTypes.ID_CONNECTION_REQUEST_ACCEPTED)
        {
            Debug.Log("Failed to connect to NAT Facilitator: " + ((DefaultMessageIDTypes)packet.data[0]));
            yield break;
        }

        // Success, we are connected to the Facilitator
        guid = rakPeer.GetMyGUID().g.ToString();
        Debug.Log("Connected: " + guid);

        // We store this for later so that we can tell which incoming messages are coming from the facilitator
        facilitatorSystemAddress = packet.systemAddress;

        // Now that we have an external connection we can get the externalIP
        externalIP = rakPeer.GetExternalID(packet.systemAddress).ToString(false);

        if (firstTimeConnect)
        {
            firstTimeConnect = false;

            // Attach RakNet punchthrough client
            // This is really what does all the heavy lifting
            natPunchthroughClient = new NatPunchthroughClient();
            rakPeer.AttachPlugin(natPunchthroughClient);
            // Punchthrough can't happen until RakNet is done finding router port stride
            // so we start it asap. If we didn't call this here RakNet would handle it
            // when we actually try and punch through.
            natPunchthroughClient.FindRouterPortStride(facilitatorSystemAddress);
        }
        else
        {
            // If this is not the first time connecting to the facilitor it means the server just received
            // a successful punchthrough and it reconnecting to prepare for more punching. We can start
            // listening immediately.
            StartCoroutine(waitForIncomingNATPunchThroughOnServer(onHolePunched));
        }

        isReady = true;
    }
    IEnumerator connectToNATFacilitator()
    {
        StartupResult startResult = rakPeer.Startup(2, new SocketDescriptor(), 1);

        if (startResult != StartupResult.RAKNET_STARTED)
        {
            NATNetworkManager_PHP.DisplayLog("Failed to initialize network interface: " + startResult.ToString());
            yield break;
        }
        ConnectionAttemptResult connectResult = rakPeer.Connect(facilitatorIP, (ushort)facilitatorPort, null, 0);

        if (connectResult != ConnectionAttemptResult.CONNECTION_ATTEMPT_STARTED)
        {
            NATNetworkManager_PHP.DisplayLog("Failed to initialize connection to NAT Facilitator: " + connectResult.ToString());
            yield break;
        }

        Packet packet;

        while ((packet = rakPeer.Receive()) == null)
        {
            yield return(new WaitForEndOfFrame());
        }

        if (packet.data[0] != (byte)DefaultMessageIDTypes.ID_CONNECTION_REQUEST_ACCEPTED)
        {
            NATNetworkManager_PHP.DisplayLog("Failed to connect to NAT Facilitator: " + ((DefaultMessageIDTypes)packet.data[0]));
            NATNetworkManager_PHP.RefreshInfo();
            yield break;
        }

        guid = rakPeer.GetMyGUID().g.ToString();
        Debug.Log("Connected: " + guid);


        externalIP = rakPeer.GetExternalID(packet.systemAddress).ToString(false);

        NATNetworkManager_PHP.RefreshInfo();



        //if (firstTimeConnect) {
        //	firstTimeConnect = false;
        natPunchthroughClient = new NatPunchthroughClient();
        rakPeer.AttachPlugin(natPunchthroughClient);

        natPunchthroughClient.FindRouterPortStride(packet.systemAddress);
        facilitatorSystemAddress = packet.systemAddress;

        /*} else {
         *      rakPeer.AttachPlugin (natPunchthroughClient);
         *      //if (!facilitatorSystemAddress.EqualsExcludingPort(packet.systemAddress)) {
         *              natPunchthroughClient.FindRouterPortStride (packet.systemAddress);
         *      //}
         *      facilitatorSystemAddress = packet.systemAddress;
         * }*/
        if (NetworkServer.active && NATNetworkManager_PHP.singleton.myRoom.isNetGame)
        {
            StartCoroutine("waitForIncomingNATPunchThroughOnServer");
        }

        if (string.IsNullOrEmpty(guid))
        {
            NATNetworkManager_PHP.DisplayLog("GUID IS NULL OR EMPTY");
        }
        else
        {
            NATNetworkManager_PHP.singleton.StartCoroutine("setNewGUID", guid);
        }

        NATNetworkManager_PHP.connectedToFac = true;
        rakPeer.SetMaximumIncomingConnections(2);
        isReady = true;
    }