private void OnAcceptted(ListeningContext context)
        {
            if (context.AcceptSocket == null || context.AcceptSocket.RemoteEndPoint == null)
            {
                // Canceled due to shutdown.
                return;
            }

#if !API_SIGNATURE_TEST
            MsgPackRpcServerProtocolsTrace.TraceEvent(
                MsgPackRpcServerProtocolsTrace.EndAccept,
                "Accept. {{ \"Socket\" : 0x{0:X}, \"RemoteEndPoint\" : \"{1}\", \"LocalEndPoint\" : \"{2}\" }}",
                ServerTransport.GetHandle(context.AcceptSocket),
                ServerTransport.GetRemoteEndPoint(context.AcceptSocket, context),
                ServerTransport.GetLocalEndPoint(context.AcceptSocket)
                );
#endif

            Contract.Assert(context.BytesTransferred == 0, context.BytesTransferred.ToString());

            var transport = this.GetTransport(context.AcceptSocket);
            context.AcceptSocket = null;
            this.Accept(context);
            transport.Receive(this.GetRequestContext(transport));
        }
        private void OnCompleted(object sender, SocketAsyncEventArgs e)
        {
            var socket = sender as Socket;

            if (!this.HandleSocketError(socket, e))
            {
                return;
            }

            switch (e.LastOperation)
            {
            case SocketAsyncOperation.Accept:
            {
                var context = e as ListeningContext;
                Contract.Assert(context != null);
                this.OnAcceptted(context);
                break;
            }

            default:
            {
#if !API_SIGNATURE_TEST
                MsgPackRpcServerProtocolsTrace.TraceEvent(
                    MsgPackRpcServerProtocolsTrace.UnexpectedLastOperation,
                    "Unexpected operation. {{ \"Socket\" : 0x{0:X}, \"RemoteEndPoint\" : \"{1}\", \"LocalEndPoint\" : \"{2}\", \"LastOperation\" : \"{3}\" }}",
                    ServerTransport.GetHandle(socket),
                    ServerTransport.GetRemoteEndPoint(socket, e),
                    ServerTransport.GetLocalEndPoint(socket),
                    e.LastOperation
                    );
#endif
                break;
            }
            }
        }
        /// <summary>
        ///		Handles the socket error as server error.
        /// </summary>
        /// <param name="socket">The <see cref="Socket"/> caused error.</param>
        /// <param name="context">The <see cref="SocketAsyncEventArgs"/> instance containing the asynchronous operation data.</param>
        /// <returns>
        ///		<c>true</c>, if the error can be ignore, it is in shutdown which is initiated by another thread, for example; otherwise, <c>false</c>.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        ///		<paramref name="socket"/> is <c>null</c>.
        ///		Or <paramref name="context"/> is <c>null</c>.
        /// </exception>
        /// <remarks>
        ///		When this method returns <c>false</c>, <see cref="RpcServer.ServerError"/> event will be also ocurred.
        /// </remarks>
        protected internal bool HandleSocketError(Socket socket, SocketAsyncEventArgs context)
        {
            if (socket == null)
            {
                throw new ArgumentNullException("socket");
            }

            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            Contract.EndContractBlock();

            bool?isError = context.SocketError.IsError();

            if (isError == null)
            {
                MsgPackRpcServerProtocolsTrace.TraceEvent(
                    MsgPackRpcServerProtocolsTrace.IgnoreableError,
                    "Ignoreable error. {{ \"Socket\" : 0x{0:X}, \"RemoteEndpoint\" : \"{1}\", \"LocalEndpoint\" : \"{2}\", \"LastOperation\" : \"{3}\", \"SocketError\" : \"{4}\", \"ErrorCode\" : 0x{5:X} }}",
                    ServerTransport.GetHandle(socket),
                    ServerTransport.GetRemoteEndPoint(socket, context),
                    ServerTransport.GetLocalEndPoint(socket),
                    context.LastOperation,
                    context.SocketError,
                    ( int )context.SocketError
                    );
                return(true);
            }
            else if (isError.GetValueOrDefault())
            {
                var errorDetail =
                    String.Format(
                        CultureInfo.CurrentCulture,
                        "Socket error. {{ \"Socket\" : 0x{0:X}, \"RemoteEndpoint\" : \"{1}\", \"LocalEndpoint\" : \"{2}\", \"LastOperation\" : \"{3}\", \"SocketError\" : \"{4}\", \"ErrorCode\" : 0x{5:X} }}",
                        ServerTransport.GetHandle(socket),
                        ServerTransport.GetRemoteEndPoint(socket, context),
                        ServerTransport.GetLocalEndPoint(socket),
                        context.LastOperation,
                        context.SocketError,
                        ( int )context.SocketError
                        );
                MsgPackRpcServerProtocolsTrace.TraceEvent(
                    MsgPackRpcServerProtocolsTrace.SocketError,
                    errorDetail
                    );

                this.RaiseServerError(new RpcTransportException(context.SocketError.ToRpcError(), "Socket error.", errorDetail, new SocketException(( int )context.SocketError)));
                return(false);
            }

            return(true);
        }