public void InternalListen(object portObj)
        {
            _endpointCollection = new EndpointCollection(_endpointTimeout, _channelCreators, _bufferCreator);
            Type = TransportType.Server;
            var port = (int)portObj;

            byte[]     data = new byte[8096];
            IPEndPoint ip   = new IPEndPoint(IPAddress.Any, port);

            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

            socket.Bind(ip);

            IPEndPoint            sender   = new IPEndPoint(IPAddress.Any, 0);
            EndPoint              endpoint = (EndPoint)(sender);
            ITeleportPacketBuffer packetBuffer;

            byte[] packetData = new byte[8096];
            int    packetLength;
            byte   channelId;
            int    receivedDataLength;

            _stopRequested = false;
            ThreadStarted  = true;
            while (!_stopRequested)
            {
                SendOutgoingDataAllChannelsOfAllEndpoints(socket, _endpointCollection);


                while (socket.Available > 0)
                {
                    receivedDataLength = socket.ReceiveFrom(data, ref endpoint);
                    _endpointCollection.Ping(endpoint);
                    packetBuffer = _endpointCollection.GetBufferOfEndpoint(endpoint);
                    packetBuffer.ReceiveRawData(data, receivedDataLength);
                    do
                    {
                        packetLength = packetBuffer.TryParseNextIncomingPacket(packetData, out channelId);
                        if (packetLength > 0)
                        {
                            ReceiveIncomingData(channelId, packetData, packetLength, endpoint, _endpointCollection);
                        }
                    }while (packetLength > 0);
                }


                Upkeep();
            }
        }
        private void InternalClient(object clientParamsObj)
        {
            _endpointCollection = new EndpointCollection(_endpointTimeout, _channelCreators, _bufferCreator);

            var    clientParams = (ClientParams)clientParamsObj;
            var    udpClient    = new UdpClient();
            Socket socket       = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            var    endpoint     = new IPEndPoint(clientParams.address, clientParams.port);

            byte[] data;
            byte[] packetData = new byte[8096];
            int    packetLength;
            ITeleportPacketBuffer packetBuffer;
            byte channelId;

            Type = TransportType.Client;
            socket.Connect(endpoint);
            _endpointCollection.Ping(endpoint);

            int receivedDataLength;

            _stopRequested = false;
            ThreadStarted  = true;
            while (!_stopRequested)
            {
                SendOutgoingDataAllChannelsOfAllEndpoints(socket, _endpointCollection);
                data = new byte[8096];

                while (socket.Available > 0)
                {
                    receivedDataLength = socket.Receive(data);
                    _endpointCollection.Ping(endpoint);
                    packetBuffer = _endpointCollection.GetBufferOfEndpoint(endpoint);
                    packetBuffer.ReceiveRawData(data, receivedDataLength);
                    do
                    {
                        packetLength = packetBuffer.TryParseNextIncomingPacket(packetData, out channelId);
                        if (packetLength > 0)
                        {
                            ReceiveIncomingData(channelId, packetData, packetLength, endpoint, _endpointCollection);
                        }
                    }while (packetLength > 0);
                }

                Upkeep();
            }
        }
        private void SendOutgoingDataAllChannelsOfAllEndpoints(Socket socket, EndpointCollection endpointCollection)
        {
            byte[] data;
            BaseTeleportChannel   channel;
            ITeleportPacketBuffer packetBuffer;

            foreach (var endpoint in endpointCollection.GetEndpoints())
            {
                packetBuffer = _endpointCollection.GetBufferOfEndpoint(endpoint);
                var endpointChannels = endpointCollection.GetChannelsOfEndpoint(endpoint);
                for (byte channelId = 0; channelId < endpointChannels.Length; channelId++)
                {
                    channel = endpointChannels[channelId];
                    while (channel.OutgoingMessageCount > 0)
                    {
                        data = channel.GetNextOutgoingData();
                        data = packetBuffer.CreatePacket(channelId, data, 0, (ushort)data.Length);
                        ClientSocketSend(socket, data, data.Length, SocketFlags.None, endpoint);
                    }
                }
            }
        }