private void Wrapper_OnPacketReceived(ConnectionSocketWrapper sender, byte[] data)
        {
            LastReceiveTime = DateTime.Now;

            StopCheckConfigTimer();
            RaiseConnectedAsync();
            lock (SyncRoot)
            {
                _isConnected = true;
            }

            Helpers.Execute.BeginOnThreadPool(() =>
            {
                RaisePacketReceived(new DataEventArgs(data));
            });
        }
        public NativeTcpTransport(string host, int port, string staticHost, int staticPort, MTProtoTransportType mtProtoType, short protocolDCId, byte[] protocolSecret, TLProxyConfigBase proxyConfig)
            : base(host, port, staticHost, staticPort, mtProtoType, proxyConfig)
        {
//            System.Diagnostics.Debug.WriteLine(
//                "  [NativeTcpTransport] .ctor begin host={0} port={1} static_host={2} static_port={3} type={4} protocol_dcid={5} protocol_secret={6} proxy={7}",
//                host, port, staticHost, staticPort, mtProtoType, protocolDCId, protocolSecret, proxyConfig);

            ActualHost = host;
            ActualPort = port;

            var           ipv4          = true;
            ProxySettings proxySettings = null;

            if (proxyConfig != null && proxyConfig.IsEnabled.Value && !proxyConfig.IsEmpty)
            {
                var socks5Proxy = proxyConfig.GetProxy() as TLSocks5Proxy;
                if (socks5Proxy != null)
                {
                    try
                    {
                        ipv4 = new HostName(socks5Proxy.Server.ToString()).Type == HostNameType.Ipv4;
                    }
                    catch (Exception ex)
                    {
                    }
                    proxySettings = new ProxySettings
                    {
                        Type     = ProxyType.Socks5,
                        Host     = socks5Proxy.Server.ToString(),
                        Port     = socks5Proxy.Port.Value,
                        Username = socks5Proxy.Username.ToString(),
                        Password = socks5Proxy.Password.ToString(),
                        IPv4     = ipv4
                    };

                    ActualHost     = staticHost;
                    ActualPort     = staticPort;
                    protocolSecret = null;
                    protocolDCId   = protocolDCId;
                }
                var mtProtoProxy = proxyConfig.GetProxy() as TLMTProtoProxy;
                if (mtProtoProxy != null)
                {
                    try
                    {
                        ipv4 = new HostName(mtProtoProxy.Server.ToString()).Type == HostNameType.Ipv4;
                    }
                    catch (Exception ex)
                    {
                    }
                    proxySettings = new ProxySettings
                    {
                        Type   = ProxyType.MTProto,
                        Host   = mtProtoProxy.Server.ToString(),
                        Port   = mtProtoProxy.Port.Value,
                        Secret = TLUtils.ParseSecret(mtProtoProxy.Secret),
                        IPv4   = ipv4
                    };

                    ActualHost = staticHost;
                    ActualPort = staticPort;
                }
            }

            try
            {
                ipv4 = new HostName(ActualHost).Type == HostNameType.Ipv4;
            }
            catch (Exception ex)
            {
            }

            var connectionSettings = new ConnectionSettings
            {
                Host           = ActualHost,
                Port           = ActualPort,
                IPv4           = ipv4,
                ProtocolDCId   = protocolDCId,
                ProtocolSecret = protocolSecret
            };

            _proxySettings = proxySettings;

//            var proxyString = proxySettings == null
//                ? "null"
//                : string.Format("[host={0} port={1} ipv4={2} type={3} secret={4} username={5} password={6}]",
//                proxySettings.Host,
//                proxySettings.Port,
//                proxySettings.IPv4,
//                proxySettings.Type,
//                proxySettings.Secret,
//                proxySettings.Username,
//                proxySettings.Password);
//            System.Diagnostics.Debug.WriteLine(
//                "  [NativeTcpTransport] .ctor end host={0} port={1} ipv4={2} protocol_dcid={3} protocol_secret={4} proxy={5}",
//                connectionSettings.Host, connectionSettings.Port, connectionSettings.IPv4, connectionSettings.ProtocolDCId, connectionSettings.ProtocolSecret, proxyString);

            _wrapper                 = new ConnectionSocketWrapper(connectionSettings, proxySettings);
            _wrapper.Closed         += Wrapper_OnClosed;
            _wrapper.PacketReceived += Wrapper_OnPacketReceived;
        }
 private void Wrapper_OnClosed(ConnectionSocketWrapper sender)
 {
     RaiseConnectionLost();
 }