/// <summary>
        /// 发送一个异步的数据信息,该方式在NET35里是不可用的。
        /// </summary>
        /// <param name="socket">网络的套接字</param>
        /// <param name="data">数据内容</param>
        /// <returns>是否发送成功</returns>
        protected OperateResult SendAsync(Socket socket, byte[] data)
        {
            if (data == null)
            {
                return(OperateResult.CreateSuccessResult());
            }
            if (data.Length == 0)
            {
                return(OperateResult.CreateSuccessResult());
            }

            var state = new StateObjectAsync <bool>(data.Length);

            state.Tcs        = new TaskCompletionSource <bool>();
            state.WorkSocket = socket;
            state.Buffer     = data;

            try
            {
                socket.BeginSend(state.Buffer, state.AlreadyDealLength, state.DataLength - state.AlreadyDealLength,
                                 SocketFlags.None, new AsyncCallback(SendAsyncCallBack), state);
                bool boolResult = state.Tcs.Task.Result;
                return(OperateResult.CreateSuccessResult());
            }
            catch (Exception ex)
            {
                return(new OperateResult(ex.Message));
            }
        }
        private OperateResult <Socket> ConnectAsync(IPEndPoint endPoint, int timeOut)
        {
            var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            var state  = new StateObjectAsync <Socket>();

            state.Tcs        = new TaskCompletionSource <Socket>();
            state.WorkSocket = socket;

            // timeout check
            TimeOut connectTimeout = new TimeOut()
            {
                WorkSocket = socket,
                DelayTime  = timeOut
            };

            ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadPoolCheckTimeOut), connectTimeout);

            try
            {
                socket.BeginConnect(endPoint, new AsyncCallback(ConnectAsyncCallBack), state);
                socket = state.Tcs.Task.Result;
                return(OperateResult.CreateSuccessResult(socket));
            }
            catch (Exception ex)
            {
                return(new OperateResult <Socket>(ex.Message));
            }
        }
        /// <summary>
        /// 接收固定长度的字节数组
        /// </summary>
        /// <remarks>
        /// Receive Special Length Bytes
        /// </remarks>
        /// <param name="socket">网络通讯的套接字</param>
        /// <param name="length">准备接收的数据长度</param>
        /// <returns>包含了字节数据的结果类</returns>
        protected OperateResult <byte[]> ReceiveAsync(Socket socket, int length)
        {
            if (length <= 0)
            {
                return(OperateResult.CreateSuccessResult(new byte[0]));
            }

            var state = new StateObjectAsync <byte[]>(length);

            state.Tcs        = new TaskCompletionSource <byte[]>();
            state.WorkSocket = socket;

            try
            {
                socket.BeginReceive(state.Buffer, state.AlreadyDealLength, state.DataLength - state.AlreadyDealLength,
                                    SocketFlags.None, new AsyncCallback(ReceiveAsyncCallback), state);
                byte[] byteResult = state.Tcs.Task.Result;
                if (byteResult == null)
                {
                    socket?.Close();
                    return(new OperateResult <byte[]>(StringResources.Language.RemoteClosedConnection));
                }

                state.Clear();
                state = null;
                return(OperateResult.CreateSuccessResult(byteResult));
            }
            catch (Exception ex)
            {
                return(new OperateResult <byte[]>(ex.Message));
            }
        }