private void client_ConnectCompleted(object sender, SocketEventArgs e)
 {
     if (e.Socket.IsConnected)
     {
         List <byte> data = new List <byte> {
             0
         };
         if (_ConnectHead == null)
         {
             data.Add(0);
             data.Add(0);
         }
         else
         {
             data.AddRange(BitConverter.GetBytes((ushort)_ConnectHead.Length));
             data.AddRange(_ConnectHead);
         }
         if (Credential != null)
         {
             if (Credential.Username == null)
             {
                 data.Add(0);
                 data.Add(0);
             }
             else
             {
                 data.AddRange(BitConverter.GetBytes((ushort)Credential.Username.Length));
                 data.AddRange(Credential.Username);
             }
             if (Credential.Password != null)
             {
                 data.AddRange(BitConverter.GetBytes((ushort)Credential.Password.Length));
                 data.AddRange(Credential.Password);
             }
         }
         e.Socket.SendAsync(data.ToArray());
     }
     else
     {
         _Connecting = false;
         CommunicationConnectEventArgs eventArgs = new CommunicationConnectEventArgs(this, false, null);
         if (ConnectCompleted != null)
         {
             ConnectCompleted(this, eventArgs);
         }
     }
 }
 private void client_ReceiveCompleted(object sender, SocketEventArgs e)
 {
     if (((e.Data[0] == 0) && (e.DataLength >= 2)) && _Connecting)
     {
         bool   success = e.Data[1] == 1;
         byte[] head    = null;
         if (!success && (e.DataLength > 2))
         {
             head = e.Data.Skip <byte>(2).ToArray <byte>();
         }
         _Connecting = false;
         CommunicationConnectEventArgs eventArgs = new CommunicationConnectEventArgs(this, success, head);
         if (ConnectCompleted != null)
         {
             ConnectCompleted(this, eventArgs);
         }
     }
 }