private void btnConnect_Click(object sender, EventArgs e)
 {
     if (btnConnect.Text.Equals("Connect"))
     {
         string hostname = tbHostname.Text;
         string port = tbPort.Text;
         tbHostname.Enabled = false;
         tbPort.Enabled = false;
         tbSend.Enabled = true;
         btnSend.Enabled = true;
         btnConnect.Text = "Disconnect";
         ClientOps ops = new ClientOps(this,hostname, port);
         m_client.Connect(ops);
     }
     else
     {
         tbHostname.Enabled = true;
         tbPort.Enabled = true;
         btnConnect.Text = "Connect";
         tbSend.Enabled = false;
         btnSend.Enabled = false;
         if (m_client.IsConnectionAlive())
             m_client.Disconnect();
     }
 }
        /// <summary>
        /// Connect to the server with given options
        /// </summary>
        /// <param name="ops">options for client</param>
        public void Connect(ClientOps ops)
        {
            if (ops == null)
            {
                ops = ClientOps.defaultClientOps;
            }
//             if (ops.CallBackObj == null)
//                 throw new NullReferenceException("callBackObj is null!");
            lock (m_generalLock)
            {
                m_clientOps = ops;
            }
            Start();
        }
 /// <summary>
 /// Connect to the server with given options
 /// </summary>
 /// <param name="ops">options for client</param>
 public void Connect(ClientOps ops)
 {
     lock (m_generalLock)
     {
         if (IsConnectionAlive())
         {
             return;
         }
     }
     if (ops == null)
     {
         ops = ClientOps.defaultClientOps;
     }
     if (ops.callBackObj == null)
     {
         throw new NullReferenceException("callBackObj is null!");
     }
     lock (m_generalLock)
     {
         m_clientOps = ops;
     }
     Start();
 }
 /// <summary>
 /// Default copy constructor
 /// </summary>
 /// <param name="b">the object to copy from</param>
 public IocpTcpClient(IocpTcpClient b)
     : base(b)
 {
     m_clientOps = b.m_clientOps;
 }
        /// <summary>
        /// Connect to the server with given options
        /// </summary>
        /// <param name="ops">options for client</param>
        public void Connect(ClientOps ops)
        {
            if (ops == null)
                ops = ClientOps.defaultClientOps;
//             if (ops.CallBackObj == null)
//                 throw new NullReferenceException("callBackObj is null!");
            lock (m_generalLock)
            {
                m_clientOps = ops;
            }
            Start();
      
        }
 /// <summary>
 /// Default copy constructor
 /// </summary>
 /// <param name="b">the object to copy from</param>
 public IocpTcpClient(IocpTcpClient b)
     : base(b)
 {
     m_clientOps = b.m_clientOps;
 }
        /// <summary>
        /// Make the connection to the server and start receiving
        /// </summary>
        protected override void execute()
        {
            ConnectStatus status = ConnectStatus.SUCCESS;
            try
            {
                lock (m_generalLock)
                {
                    if (IsConnectionAlive)
                    {
                        status = ConnectStatus.FAIL_ALREADY_CONNECTED;
                        throw new CallbackException();
                    }

                    Guid = Guid.NewGuid();

                    CurSocketCount = 0;


                    CallBackObj = m_clientOps.CallBackObj;
                    HostName = m_clientOps.HostName;
                    Port = m_clientOps.Port;
                    ReceiveType = m_clientOps.ReceiveType;
                    MaxSocketCount = m_clientOps.MaxSocketCount;
                    ConnectionTimeOut = m_clientOps.ConnectionTimeOut;

                    m_curPacketSequence = 0;
                    m_clientSet.Clear();
                    m_curConnectionTry = 0;


                    lock (m_sendLock)
                    {
                        m_pendingClientSet.Clear();

                        m_packetQueue.Clear();
                        m_pendingPacketSet.Clear();
                        m_errorPacketSet.Clear();
                    }
                    lock (m_receiveLock)
                    {
                        m_curReceivedPacketId = -1;
                        m_receivedQueue.Clear();
                    }


                    m_sendReadyEvent.ResetEvent();

                    if (HostName == null || HostName.Length == 0)
                    {
                        HostName = ServerConf.DEFAULT_HOSTNAME;
                    }

                    if (Port == null || Port.Length == 0)
                    {
                        Port = ServerConf.DEFAULT_PORT;
                    }
                    ClientOps clientOps = new ClientOps(this, HostName, Port, true, ConnectionTimeOut);
                    for (int i = 0; i < MaxSocketCount; i++)
                    {
                        INetworkClient client = new IocpTcpClient();
                        client.Connect(clientOps);
                    }
                    IsConnectionAlive = true;
                }
            }
            catch (CallbackException)
            {
                
                Task t = new Task(delegate()
                {
                    OnParallelClientConnected(this, status);
                });
                t.Start();


                return;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message + " >" + ex.StackTrace);
                Task t = new Task(delegate()
                {
                    OnParallelClientConnected(this, ConnectStatus.FAIL_SOCKET_ERROR);
                });
                t.Start();

                return;
            }
            startSend();
        }
   /// <summary>
   /// Connect to the server with given options
   /// </summary>
   /// <param name="ops">options for client</param>
   public void Connect(ClientOps ops)
   {
       lock (m_generalLock)
       {
           if (IsConnectionAlive())
               return;
       }
       if (ops == null)
           ops = ClientOps.defaultClientOps;
       if (ops.callBackObj == null)
           throw new NullReferenceException("callBackObj is null!");
       lock (m_generalLock)
       {
           m_clientOps = ops;
       }
       Start();
 
   }