static public async Task <UDPClientStream> ConnectAsync(UDPClientListener udp, string ip, int port, TimeSpan timeout)
        {
            var s = new UDPClientStream();

            s._server = new IPEndPoint(IPAddress.Parse(ip), port);

            Exception error             = null;
            CancellationTokenSource cts = new CancellationTokenSource();

            RunInNewThread(delegate
            {
                try
                {
                    s._Connect(udp, ip, port, timeout);
                }
                catch (Exception x)
                {
                    error = x;
                }
                cts.Cancel();
            });
            await cts.Token.WaitForSignalSettedAsync(-1);

            if (error != null)
            {
                throw new Exception(error.Message, error);
            }

            return(s);
        }
        void _Connect(UDPClientListener udp, string ip, int port, TimeSpan timeout)
        {
            _udp = udp;

StartConnect:

            SessionId = Interlocked.Increment(ref _nextsessionid);

            string guid = Guid.NewGuid().ToString();

            byte[] buffconnect = UDPMeta.CreateSessionConnect(SessionId, guid);
            SendToPeer(buffconnect); SendToPeer(buffconnect);
            while (true)
            {
                byte[] buffprepair = _udp.Receive(SessionId, TimeSpan.FromMilliseconds(500));
                if (buffprepair == null)
                {
                    CheckTimeout(timeout);
                    Console.WriteLine("Client StartConnect Again");
                    //SendToPeer(buffconnect); SendToPeer(buffconnect);
                    //continue;
                    goto StartConnect;
                }
                UDPPackageType ptprepair = UDPMeta.GetPackageType(buffprepair);
                if (ptprepair != UDPPackageType.SessionPrepair)
                {
                    if (ptprepair > UDPPackageType.SessionPrepair)
                    {
                        goto StartConnect;
                    }
                    continue;
                }
                long sidprepair = BitConverter.ToInt64(buffprepair, 8);
                if (sidprepair != SessionId)
                {
                    goto StartConnect;
                }
                UDPConnectJson udpc = UDPConnectJson.Deserialize(Encoding.UTF8.GetString(buffprepair, 16, buffprepair.Length - 16));
                if (udpc.token != guid)
                {
                    goto StartConnect;
                }
                break;
            }
            byte[] buffconfirm = UDPMeta.CreateSessionConfirm(SessionId, guid);
            SendToPeer(buffconfirm); SendToPeer(buffconfirm);
            while (true)
            {
                byte[] buffready = _udp.Receive(SessionId, TimeSpan.FromMilliseconds(500));
                if (buffready == null)
                {
                    CheckTimeout(timeout);
                    SendToPeer(buffconfirm); SendToPeer(buffconfirm);
                    continue;
                }
                UDPPackageType ptprepair = UDPMeta.GetPackageType(buffready);
                if (ptprepair != UDPPackageType.SessionReady)
                {
                    if (ptprepair > UDPPackageType.SessionReady)
                    {
                        goto StartConnect;
                    }
                    continue;
                }
                long sidprepair = BitConverter.ToInt64(buffready, 8);
                if (sidprepair != SessionId)
                {
                    goto StartConnect;
                }
                UDPConnectJson udpc = UDPConnectJson.Deserialize(Encoding.UTF8.GetString(buffready, 16, buffready.Length - 16));
                if (udpc.token != guid)
                {
                    goto StartConnect;
                }
                break;
            }

            _reader = new ClientBufferReader();
            Thread t = new Thread(ClientWorkThread);            //TODO:Convert to async

            t.IsBackground = true;
            t.Start();
        }