public override SocketResult Connect(byte[] data)
 {
     SocketResult sr = new SocketResult();
     EndPoint remoteEndPoint = new IPEndPoint(IPAddress.Parse(ServerIpAddress), ServerPort);
     try
     {
         if (m_Socket.SendTo(data, remoteEndPoint) == data.Length)
         {
             int recvNum = m_Socket.ReceiveFrom(m_SocketBuffer, ref remoteEndPoint);
             string dataServer = ASCIIEncoding.ASCII.GetString(m_SocketBuffer, 0, recvNum);
             DataServerPort = int.Parse(dataServer.Split(':')[1]);
             sr.Result = recvNum > 0;
             if (sr.Result && OnConnected != null)
             {
                 OnConnected(this, new ConnectedEventArgs(remoteEndPoint, m_SocketBuffer));
             }
             Array.Clear(m_SocketBuffer, 0, m_SocketBuffer.Length);
         }
     }
     catch (Exception ex)
     {
         sr.Desc = ex.Message;
     }
     return sr;
 }
 public override SocketResult Connect(byte[] data = null)
 {
     SocketResult sr = new SocketResult();
     IPEndPoint remoteEndPoint = new IPEndPoint(IPAddress.Parse(ServerIpAddress), ServerPort);
     try
     {
         m_Socket.Connect(remoteEndPoint);
         sr.Result = true;
         if (OnConnected != null)
         {
             OnConnected(this, new ConnectedEventArgs(remoteEndPoint, m_SocketBuffer));
         }
     }
     catch (Exception ex)
     {
         sr.Desc = ex.Message;
     }
     return sr;
 }
 public override SocketResult Connect(byte[] data)
 {
     SocketResult sr = new SocketResult();
     EndPoint remoteEndPoint = new IPEndPoint(IPAddress.Parse(ServerIpAddress), ServerPort);
     try
     {
         if (m_Socket.SendTo(data, remoteEndPoint) == data.Length)
         {
             int recvNum = m_Socket.ReceiveFrom(m_SocketBuffer, ref remoteEndPoint);
             if (recvNum>0 && OnConnected != null)
             {
                 OnConnected(this, new ConnectedEventArgs(remoteEndPoint, m_SocketBuffer));
             }
             Array.Clear(m_SocketBuffer, 0, m_SocketBuffer.Length);
         }
     }
     catch (Exception ex)
     {
         sr.Desc = ex.Message;
     }
     return sr;
 }
 public override SocketResult DisConnect()
 {
     SocketResult sr = new SocketResult();
     try
     {
         m_Socket.Shutdown( SocketShutdown.Both);
         sr.Result = true;
     }
     catch (Exception ex)
     {
         sr.Desc = ex.Message;
     }
     return sr;
 }
 public override SocketResult RequestSync(byte[] requestMsg, ref byte[] responseMsg)
 {
     SocketResult sr = new SocketResult();
     if (string.IsNullOrEmpty(ServerIpAddress) || ServerPort == 0)
     {
         sr.Desc = "IP or Port not Set";
     }
     else if (!m_Socket.Connected)
     {
         sr.Desc = "Server not connected";
     }
     else if (responseMsg == null)
     {
         sr.Desc = "Recv response buffer is null";
     }
     else
     {
         Array.Clear(responseMsg, 0, responseMsg.Length);
         if (m_Socket.Send(requestMsg) == requestMsg.Length)
         {
             sr.Result = m_Socket.Receive(responseMsg) > 0;
         }
     }
     return sr;
 }
 public override SocketResult RequestAsync(byte[] requestMsg)
 {
     SocketResult sr = new SocketResult();
     if (string.IsNullOrEmpty(ServerIpAddress) || ServerPort == 0)
     {
         sr.Desc = "IP or Port not Set";
     }
     else if (!m_Socket.Connected)
     {
         sr.Desc = "Server not connected";
     }
     else if (OnResponseRecv == null)
     {
         sr.Desc = "not bind event on Async call";
     }
     else
     {
         m_SocketAsyncEventArgs.SetBuffer(m_SocketBuffer, 0, requestMsg.Length);
         Buffer.BlockCopy(requestMsg, 0, m_SocketBuffer, 0, requestMsg.Length);
    
         if (!m_Socket.SendAsync(m_SocketAsyncEventArgs))
         {
             ProcessRecv(m_SocketAsyncEventArgs);
         }
         sr.Result = true;
     }
     return sr;
 }
 public override SocketResult RequestSync(byte[] requestMsg, ref byte[] responseMsg)
 {
     SocketResult sr = new SocketResult();
     EndPoint remoteEndPoint = new IPEndPoint(IPAddress.Parse(ServerIpAddress), DataServerPort);
     if (string.IsNullOrEmpty(ServerIpAddress) || DataServerPort == 0)
     {
         sr.Desc = "IP or Port not Set";
     }
     else if (responseMsg == null)
     {
         sr.Desc = "Recv response buffer is null";
     }
     else
     {
         Array.Clear(responseMsg, 0, responseMsg.Length);
         if (m_Socket.SendTo(requestMsg, remoteEndPoint) == requestMsg.Length)
         {
             sr.Result = m_Socket.ReceiveFrom(responseMsg, ref remoteEndPoint) > 0;
         }
     }
     return sr;
 }
 public override SocketResult RequestAsync(byte[] requestMsg)
 {
     SocketResult sr = new SocketResult();
     IPEndPoint remoteEndPoint = new IPEndPoint(IPAddress.Parse(ServerIpAddress), DataServerPort);
     if (string.IsNullOrEmpty(ServerIpAddress) || ServerPort == 0)
     {
         sr.Desc = "IP or Port not Set";
     }
     else if (OnResponseRecv == null)
     {
         sr.Desc = "not bind event on Async call";
     }
     else
     {
         m_SocketAsyncEventArgs.RemoteEndPoint = remoteEndPoint;
         m_SocketAsyncEventArgs.SetBuffer(m_SocketBuffer, 0, requestMsg.Length);
         Buffer.BlockCopy(requestMsg, 0, m_SocketBuffer, 0, requestMsg.Length);
         if (!m_Socket.SendToAsync(m_SocketAsyncEventArgs))
         {
             ProcessRecv(m_SocketAsyncEventArgs);
         }
         sr.Result = true;
     }
     return sr;
 }
 public override SocketResult DisConnect()
 {
     SocketResult sr = new SocketResult();
     try
     {
         DataServerPort = 0;
         sr.Result = true;
     }
     catch (Exception ex)
     {
         sr.Desc = ex.Message;
     }
     return sr;
 }