private void p2pNetwork_OnPeerConnected(PeerToPeerNetwork Listener, PeerConnection Peer)
        {
            IPEndPoint Endpoint = (IPEndPoint)Peer.Tcp.Client.RemoteEndPoint;

#if LineListener
            Console.Out.WriteLine("Receiving connection from " + Endpoint.ToString());
#endif

            lock (this.remotePlayersByEndpoint)
            {
                if (!this.remotePlayerIPs.ContainsKey(Endpoint.Address))
                {
                    Peer.Dispose();
                    return;
                }
            }

            Peer.OnClosed   += new EventHandler(Peer_OnClosed);
            Peer.OnReceived += new BinaryEventHandler(Peer_OnReceived);

            BinaryOutput Output = new BinaryOutput();

            Output.WriteGuid(this.localPlayer.PlayerId);
            Output.WriteString(this.ExternalEndpoint.Address.ToString());
            Output.WriteUInt16((ushort)this.ExternalEndpoint.Port);

            Peer.SendTcp(Output.GetPacket());
        }
        private void Serialize(Player Player, BinaryOutput Output)
        {
            Output.WriteString(Player.PublicEndpoint.Address.ToString());
            Output.WriteUInt16((ushort)Player.PublicEndpoint.Port);

            Output.WriteString(Player.LocalEndpoint.Address.ToString());
            Output.WriteUInt16((ushort)Player.LocalEndpoint.Port);

            Output.WriteGuid(Player.PlayerId);
            Output.WriteUInt((uint)Player.Count);

            foreach (KeyValuePair <string, string> P in Player)
            {
                Output.WriteString(P.Key);
                Output.WriteString(P.Value);
            }
        }
        private void Connection_OnReceived(object Sender, byte[] Packet)
        {
            PeerConnection Connection = (PeerConnection)Sender;
            Guid           PlayerId;
            IPAddress      PlayerRemoteAddress;
            IPEndPoint     PlayerRemoteEndpoint;

            try
            {
                BinaryInput Input = new BinaryInput(Packet);

                PlayerId             = Input.ReadGuid();
                PlayerRemoteAddress  = IPAddress.Parse(Input.ReadString());
                PlayerRemoteEndpoint = new IPEndPoint(PlayerRemoteAddress, Input.ReadUInt16());
            }
            catch (Exception)
            {
                Connection.Dispose();
                return;
            }

            Player Player = (Player)Connection.StateObject;
            Player Player2;

            lock (this.remotePlayersByEndpoint)
            {
                if (!this.playersById.TryGetValue(PlayerId, out Player2) || Player2.PlayerId != Player.PlayerId)
                {
                    Connection.Dispose();
                    return;
                }

                Player.Connection = Connection;
            }

            Connection.RemoteEndpoint = Player.GetExpectedEndpoint(this.p2pNetwork);

            Connection.OnReceived -= new BinaryEventHandler(Connection_OnReceived);
            Connection.OnReceived += new BinaryEventHandler(Peer_OnReceived);

            Connection.OnSent += new BinaryEventHandler(Connection_OnSent);

            BinaryOutput Output = new BinaryOutput();

            Output.WriteGuid(this.localPlayer.PlayerId);
            Output.WriteString(this.ExternalAddress.ToString());
            Output.WriteUInt16((ushort)this.ExternalEndpoint.Port);

            Connection.SendTcp(Output.GetPacket());

            MultiPlayerEnvironmentPlayerInformationEventHandler h = this.OnPlayerConnected;

            if (!(h is null))
            {
                try
                {
                    h(this, Player);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                    Debug.WriteLine(ex.StackTrace.ToString());
                }
            }
        }
		private void Connection_OnReceived(object Sender, byte[] Packet)
		{
			PeerConnection Connection = (PeerConnection)Sender;
			Guid PlayerId;
			IPAddress PlayerRemoteAddress;
			IPEndPoint PlayerRemoteEndpoint;

			try
			{
				BinaryInput Input = new BinaryInput(Packet);

				PlayerId = Input.ReadGuid();
				PlayerRemoteAddress = IPAddress.Parse(Input.ReadString());
				PlayerRemoteEndpoint = new IPEndPoint(PlayerRemoteAddress, Input.ReadUInt16());
			}
			catch (Exception)
			{
				Connection.Dispose();
				return;
			}

			Player Player = (Player)Connection.StateObject;
			Player Player2;

			lock (this.remotePlayersByEndpoint)
			{
				if (!this.playersById.TryGetValue(PlayerId, out Player2) || Player2.PlayerId != Player.PlayerId)
				{
					Connection.Dispose();
					return;
				}

				Player.Connection = Connection;
			}

			Connection.RemoteEndpoint = Player.GetExpectedEndpoint(this.p2pNetwork);

			Connection.OnReceived -= new BinaryEventHandler(Connection_OnReceived);
			Connection.OnReceived += new BinaryEventHandler(Peer_OnReceived);

			Connection.OnSent += new BinaryEventHandler(Connection_OnSent);

			BinaryOutput Output = new BinaryOutput();

			Output.WriteGuid(this.localPlayer.PlayerId);
			Output.WriteString(this.ExternalAddress.ToString());
			Output.WriteUInt16((ushort)this.ExternalEndpoint.Port);

			Connection.SendTcp(Output.GetPacket());

			MultiPlayerEnvironmentPlayerInformationEventHandler h = this.OnPlayerConnected;
			if (h != null)
			{
				try
				{
					h(this, Player);
				}
				catch (Exception ex)
				{
					Debug.WriteLine(ex.Message);
					Debug.WriteLine(ex.StackTrace.ToString());
				}
			}
		}
		private void p2pNetwork_OnPeerConnected(PeerToPeerNetwork Listener, PeerConnection Peer)
		{
			IPEndPoint Endpoint = (IPEndPoint)Peer.Tcp.Client.RemoteEndPoint;

#if LineListener
			Console.Out.WriteLine("Receiving connection from " + Endpoint.ToString());
#endif

			lock (this.remotePlayersByEndpoint)
			{
				if (!this.remotePlayerIPs.ContainsKey(Endpoint.Address))
				{
					Peer.Dispose();
					return;
				}
			}

			Peer.OnClosed += new EventHandler(Peer_OnClosed);
			Peer.OnReceived += new BinaryEventHandler(Peer_OnReceived);

			BinaryOutput Output = new BinaryOutput();

			Output.WriteGuid(this.localPlayer.PlayerId);
			Output.WriteString(this.ExternalEndpoint.Address.ToString());
			Output.WriteUInt16((ushort)this.ExternalEndpoint.Port);

			Peer.SendTcp(Output.GetPacket());
		}
		private void Serialize(Player Player, BinaryOutput Output)
		{
			Output.WriteString(Player.PublicEndpoint.Address.ToString());
			Output.WriteUInt16((ushort)Player.PublicEndpoint.Port);

			Output.WriteString(Player.LocalEndpoint.Address.ToString());
			Output.WriteUInt16((ushort)Player.LocalEndpoint.Port);

			Output.WriteGuid(Player.PlayerId);
			Output.WriteUInt((uint)Player.Count);

			foreach (KeyValuePair<string, string> P in Player)
			{
				Output.WriteString(P.Key);
				Output.WriteString(P.Value);
			}
		}