public void ConnectAsync(int port, string ip)
 {
     try
     {
         if (connected == false || sock == null || sock.Connected == false)
         {
             Close();
         }
         connected   = false;
         channelType = ClientChannelType.Async;
         using (LockWait wait = new LockWait(ref _lockParam))
         {
             CreateTcpSocket(port, ip);
             SocketAsyncEventArgs conArg = new SocketAsyncEventArgs
             {
                 RemoteEndPoint = endPoint,
                 UserToken      = new SocketToken(-1)
                 {
                     sock = sock
                 }
             };
             conArg.AcceptSocket = sock;
             conArg.Completed   += new EventHandler <SocketAsyncEventArgs>(IOCompleted);
             if (!sock.ConnectAsync(conArg))
             {
                 ConnectCallback(conArg);
             }
         }
     }
     catch (Exception)
     {
         Close();
         throw;
     }
 }
        public bool ConnectSync(int port, string ip)
        {
            if (connected == false || sock == null || sock.Connected == false)
            {
                Close();
            }
            connected   = false;
            channelType = ClientChannelType.Sync;
            int retry = 3;

            using (LockWait wait = new LockWait(ref _lockParam))
            {
                CreateTcpSocket(port, ip);
            }
            while (retry > 0)
            {
                try
                {
                    --retry;
                    sock.Connect(endPoint);
                    connected = true;
                    return(true);
                }
                catch (Exception)
                {
                    Close();
                    if (retry <= 0)
                    {
                        throw;
                    }
                    Thread.Sleep(1000);
                }
            }
            return(false);
        }
 public bool ConnectTo(int port, string ip)
 {
     try
     {
         if (connected == false || sock == null || sock.Connected == false)
         {
             Close();
         }
         connected   = false;
         channelType = ClientChannelType.AsyncWait;
         using (LockWait lwait = new LockWait(ref _lockParam))
         {
             CreateTcpSocket(port, ip);
             //连接事件绑定
             var sArgs = new SocketAsyncEventArgs
             {
                 RemoteEndPoint = endPoint,
                 UserToken      = new SocketToken()
                 {
                     sock = sock
                 }
             };
             sArgs.AcceptSocket = sock;
             sArgs.Completed   += new EventHandler <SocketAsyncEventArgs>(IOCompleted);
             if (!sock.ConnectAsync(sArgs))
             {
                 ConnectCallback(sArgs);
             }
         }
         _autoReset.WaitOne(connTimeout);
         connected = sock.Connected;
         return(connected);
     }
     catch (Exception)
     {
         Close();
         throw;
     }
 }