/// <summary>
        /// Set up an RPC session with a specific server.
        /// </summary>
        /// <param name="serverName">Server computer name</param>
        /// <param name="credential">User account used to setup this session</param>
        /// <param name="securityContext">Security context of session</param>
        public void Bind(
            string serverName,
            AccountCredential credential,
            ClientSecurityContext securityContext)
        {
            if (string.IsNullOrEmpty(serverName))
            {
                throw new ArgumentNullException("serverName");
            }

            InnerBind(
                RpceUtility.RPC_OVER_NAMED_PIPE_PROTOCOL_SEQUENCE,
                serverName,
                SrvsUtility.SRVS_NAMED_PIPE,
                credential,
                securityContext);
        }
 /// <summary>
 /// Bind to SAMR RPC server.
 /// </summary>
 /// <param name="protocolSequence">
 /// RPC protocol sequence.
 /// </param>
 /// <param name="networkAddress">
 /// RPC network address.
 /// </param>
 /// <param name="endpoint">
 /// RPC endpoint.
 /// </param>
 /// <param name="transportCredential">
 /// If connect by SMB/SMB2, it's the security credential 
 /// used by under layer transport (SMB/SMB2). 
 /// If connect by TCP, this parameter is ignored.
 /// </param>
 /// <param name="securityContext">
 /// RPC security provider.
 /// </param>
 /// <param name="authenticationLevel">
 /// RPC authentication level.
 /// </param>
 /// <param name="timeout">
 /// Timeout
 /// </param>
 public void Bind(
     string protocolSequence,
     string networkAddress,
     string endpoint,
     AccountCredential transportCredential,
     ClientSecurityContext securityContext,
     RpceAuthenticationLevel authenticationLevel,
     TimeSpan timeout)
 {
     rpc.Bind(protocolSequence,
                     networkAddress,
                     endpoint,
                     transportCredential,
                     securityContext,
                     authenticationLevel,
                     timeout);
 }
        /// <summary>
        /// RPC bind over named pipe, using well-known endpoint "\\pipe\\FssagentRpc".
        /// </summary>
        /// <param name="serverName">FSRVP server machine name.</param>
        /// <param name="transportCredential">
        /// If connect by SMB/SMB2, it's the security credential 
        /// used by underlayer transport (SMB/SMB2). 
        /// If connect by TCP, this parameter is ignored.
        /// </param>
        /// <param name="securityContext">
        /// Security provider for RPC. 
        /// Set the value to null to disable authentication.
        /// </param>
        /// <param name="timeout">Timeout for bind and all future requests.</param>
        /// <returns>Return true if success, otherwise return false.</returns>
        /// <exception cref="ArgumentNullException">
        /// Thrown when serverName is null.
        /// </exception>
        public void BindOverNamedPipe(
            string serverName,
            AccountCredential transportCredential,
            ClientSecurityContext securityContext,
            TimeSpan timeout)
        {
            if (serverName == null)
            {
                throw new ArgumentNullException("serverName");
            }

            Bind(
                RpceUtility.RPC_OVER_NAMED_PIPE_PROTOCOL_SEQUENCE,
                serverName,
                FsrvpUtility.FSRVP_NAMED_PIPE,
                transportCredential,
                securityContext,
                context.AuthenticationLevel,
                timeout);
        }
        /// <summary>
        /// RPC bind to interface, using specified endpoint and authenticate provider.
        /// </summary>
        /// <param name="protocolSequence">RPC protocol sequence.</param>
        /// <param name="networkAddress">RPC network address.</param>
        /// <param name="endpoint">RPC endpoint.</param>
        /// <param name="transportCredential">
        /// If connect by SMB/SMB2, it's the security credential 
        /// used by underlayer transport (SMB/SMB2). 
        /// If connect by TCP, this parameter is ignored.
        /// </param>
        /// <param name="securityContext">RPC security provider.</param>
        /// <param name="authenticationLevel">RPC authentication level.</param>
        /// <param name="timeout">Timeout for bind and all future requests.</param>
        /// <exception cref="InvalidOperationException">
        /// Thrown when rpceClientTransport is not null.
        /// </exception>
        public void Bind(
            string protocolSequence,
            string networkAddress,
            string endpoint,
            AccountCredential transportCredential,
            ClientSecurityContext securityContext,
            RpceAuthenticationLevel authenticationLevel,
            TimeSpan timeout)
        {
            if (rpceClientTransport != null)
            {
                throw new InvalidOperationException("FSRVP has already been bind.");
            }

            rpceTimeout = timeout;

            rpceClientTransport = new RpceClientTransport();

            try
            {
                rpceClientTransport.Bind(
                    protocolSequence,
                    networkAddress,
                    endpoint,
                    transportCredential,
                    FsrvpUtility.FSRVP_INTERFACE_UUID,
                    FsrvpUtility.FSRVP_INTERFACE_MAJOR_VERSION,
                    FsrvpUtility.FSRVP_INTERFACE_MINOR_VERSION,
                    securityContext,
                    authenticationLevel,
                    true,
                    rpceTimeout);
            }
            catch
            {
                rpceClientTransport = null;
                throw;
            }
        }
        /// <summary>
        /// Bind to SAMR RPC server.
        /// </summary>
        /// <param name="protocolSequence">
        /// RPC protocol sequence.
        /// </param>
        /// <param name="networkAddress">
        /// RPC network address.
        /// </param>
        /// <param name="endpoint">
        /// RPC endpoint.
        /// </param>
        /// <param name="transportCredential">
        /// If connect by SMB/SMB2, it's the security credential 
        /// used by under layer transport (SMB/SMB2). 
        /// If connect by TCP, this parameter is ignored.
        /// </param>
        /// <param name="securityContext">
        /// RPC security provider.
        /// </param>
        /// <param name="authenticationLevel">
        /// RPC authentication level.
        /// </param>
        /// <param name="timeout">
        /// Timeout
        /// </param>
        public void Bind(
            string protocolSequence,
            string networkAddress,
            string endpoint,
            AccountCredential transportCredential,
            ClientSecurityContext securityContext,
            RpceAuthenticationLevel authenticationLevel,
            TimeSpan timeout)
        {
            if (rpceClientTransport != null)
            {
                throw new InvalidOperationException("SAMR has already been binded.");
            }

            rpceTimeout = timeout;
            rpceClientTransport = new RpceClientTransport();

            rpceClientTransport.Bind(
                protocolSequence,
                networkAddress,
                endpoint,
                transportCredential,
                SamrUtility.SAMR_RPC_INTERFACE_UUID,
                SamrUtility.SAMR_RPC_INTERFACE_MAJOR_VERSION,
                SamrUtility.SAMR_RPC_INTERFACE_MINOR_VERSION,
                securityContext,
                authenticationLevel,
                false,
                rpceTimeout);
        }
        /// <summary>
        /// create the Sicily bind request, <para/>
        /// which is sent to server to request a sicily bind, windows implements this as NTLM authenticate.<para/>
        /// this is the first roundtrip, which contains the Sicily Discovery request.
        /// </summary>
        /// <param name="securityContext">
        /// a ClientSecurityContext object that specifies the security provider.
        /// </param>
        /// <param name="enableMessageSecurity">
        /// a bool value that indicates whether enable message security.
        /// </param>
        /// <returns>
        /// a BindRequestPacket object that requests the Sicily bind.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// thrown when securityContext is null.
        /// </exception>
        public AdtsBindRequestPacket CreateSicilyRequest(
            ClientSecurityContext securityContext, bool enableMessageSecurity)
        {
            if (securityContext == null)
            {
                throw new ArgumentNullException("securityContext");
            }

            if (this.security == null)
            {
                this.usingMessageSecurity = enableMessageSecurity;
                this.security = new AdtsLdapSaslSecurityLayer(securityContext);
            }

            if (securityContext.NeedContinueProcessing)
            {
                return this.CreateSicilyNegotiateBindRequest(SICILY_AUTH_NAME_NTLM, securityContext.Token);
            }
            else
            {
                return this.CreateSicilyResponseBindRequest(securityContext.Token);
            }
        }
        private void InnerBind(
            string protocolSequence,
            string networkAddress,
            string endpoint,
            AccountCredential transportCredential,
            ClientSecurityContext securityContext)
        {
            if (RpceClientTransport != null)
            {
                throw new InvalidOperationException("SRVS has already been bind");
            }

            RpceClientTransport = new RpceClientTransport();

            try
            {
                RpceClientTransport.Bind(
                    protocolSequence,
                    networkAddress,
                    endpoint,
                    transportCredential,
                    SrvsUtility.SRVS_INTERFACE_UUID,
                    SrvsUtility.SRVS_INTERFACE_MAJOR_VERSION,
                    SrvsUtility.SRVS_INTERFACE_MINOR_VERSION,
                    securityContext,
                    AuthenticationLevel,
                    true,
                    RpceTimeout);
            }
            catch
            {
                RpceClientTransport = null;
                throw;
            }
        }
        /// <summary>
        /// create the mutual authentication request<para/>
        /// which is sent to server to complete the mutual authentication.
        /// </summary>
        /// <param name="securityContext">
        /// a ClientSecurityContext object that specifies the security provider.<para/>
        /// the SecurityPackageType must be SecurityPackageType.Negotiate.
        /// </param>
        /// <param name="enableMessageSecurity">
        /// a bool value that indicates whether enable message security.
        /// </param>
        /// <returns>
        /// a BindRequestPacket object that requests the mutual authentication.
        /// </returns>
        /// <exception cref="InvalidOperationException">
        /// if mutual authenticate mode, only SecurityPackageType.Negotiate is available
        /// </exception>
        /// <exception cref="ArgumentNullException">
        /// thrown when securityContext is null.
        /// </exception>
        public AdtsBindRequestPacket CreateMutualAuthenticationRequest(
            ClientSecurityContext securityContext, bool enableMessageSecurity)
        {
            if (securityContext == null)
            {
                throw new ArgumentNullException("securityContext");
            }

            if (securityContext.PackageType != SecurityPackageType.Negotiate)
            {
                throw new InvalidOperationException(
                    "if mutual authenticate mode, only SecurityPackageType.Negotiate is available");
            }

            return this.CreateSaslBindRequest(securityContext, enableMessageSecurity);
        }
        /// <summary>
        /// create the SASL bind request,<para/>
        /// which is sent to server to request a SASL bind to provide more strong security.
        /// </summary>
        /// <param name="securityContext">
        /// a ClientSecurityContext object that specifies the security provider.
        /// </param>
        /// <param name="enableMessageSecurity">
        /// a bool value that indicates whether enable message security.
        /// </param>
        /// <returns>
        /// a BindRequestPacket object that requests the SASL bind.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// thrown when securityContext is null.
        /// </exception>
        public AdtsBindRequestPacket CreateSaslBindRequest(
            ClientSecurityContext securityContext, bool enableMessageSecurity)
        {
            if (securityContext == null)
            {
                throw new ArgumentNullException("securityContext");
            }

            if (this.security == null)
            {
                this.usingMessageSecurity = enableMessageSecurity;
                this.security = new AdtsLdapSaslSecurityLayer(securityContext);
            }

            return this.CreateSaslBindRequest(SASL_MECHANISM_GSS_SPNEGO, securityContext.Token);
        }
        /// <summary>
        /// RPC bind over TCP/IP, using specified endpoint and authenticate provider.
        /// </summary>
        /// <param name="serverName">NRPC server machine name.</param>
        /// <param name="endpoint">RPC endpoints, it's the port on TCP/IP.</param>
        /// <param name="securityContext">
        /// Security provider for RPC. 
        /// Set the value to null to disable authentication.
        /// </param>
        /// <param name="timeout">Timeout for bind and all future requests.</param>
        /// <exception cref="ArgumentNullException">
        /// Thrown when serverName is null.
        /// </exception>
        public void BindOverTcp(
            string serverName,
            ushort endpoint,
            ClientSecurityContext securityContext,
            TimeSpan timeout)
        {
            if (serverName == null)
            {
                throw new ArgumentNullException("serverName");
            }

            nrpcSecurityContext = securityContext as NrpcClientSecurityContext;
            if (nrpcSecurityContext != null)
            {
                context = nrpcSecurityContext.nrpc.context;
            }

            rpc.Bind(
                RpceUtility.RPC_OVER_TCPIP_PROTOCOL_SEQUENCE,
                serverName,
                endpoint.ToString(CultureInfo.InvariantCulture),
                null,
                securityContext,
                securityContext == null
                    ? RpceAuthenticationLevel.RPC_C_AUTHN_LEVEL_NONE
                    : (context.SealSecureChannel
                        ? RpceAuthenticationLevel.RPC_C_AUTHN_LEVEL_PKT_PRIVACY
                        : RpceAuthenticationLevel.RPC_C_AUTHN_LEVEL_PKT_INTEGRITY),
                timeout);

            context.PrimaryName = serverName;
            NrpcRpcAdapter nrpcRpcAdapter = rpc as NrpcRpcAdapter;
            if (nrpcRpcAdapter != null)
            {
                context.rpceTransportContext = nrpcRpcAdapter.rpceClientTransport.Context;
            }
        }
        /// <summary>
        /// Set the security provider and authentication level of the RPCE connection.
        /// </summary>
        /// <param name="securityContext">
        /// A security provider.
        /// </param>
        /// <param name="authenticationLevel">
        /// An authentication level.
        /// </param>
        /// <param name="authenticationContextId">
        /// Authentication context id, can be any value, 
        /// but must be different in different security context.
        /// </param>
        /// <exception cref="ArgumentException">
        /// Thrown when securityProvider is null but authenticationLevel is not NONE.
        /// </exception>
        /// <exception cref="NotSupportedException">
        /// Thrown when securityProvider is not supported.
        /// </exception>
        public void SetAuthInfo(
            ClientSecurityContext securityContext,
            RpceAuthenticationLevel authenticationLevel,
            uint authenticationContextId)
        {
            if (securityContext == null)
            {
                if (authenticationLevel != RpceAuthenticationLevel.RPC_C_AUTHN_LEVEL_NONE)
                {
                    throw new ArgumentException(
                        "auth_level must set to NONE when securityProvider is null",
                        "authenticationLevel");
                }

                context.AuthenticationType = RpceAuthenticationType.RPC_C_AUTHN_NONE;
            }
            else
            {
                securityContext.Initialize(null);

                switch (securityContext.PackageType)
                {
                    case SecurityPackageType.Kerberos:
                        context.AuthenticationType = RpceAuthenticationType.RPC_C_AUTHN_GSS_KERBEROS;
                        break;

                    case SecurityPackageType.Ntlm:
                        context.AuthenticationType = RpceAuthenticationType.RPC_C_AUTHN_WINNT;
                        break;

                    case SecurityPackageType.Negotiate:
                        context.AuthenticationType = RpceAuthenticationType.RPC_C_AUTHN_GSS_NEGOTIATE;
                        break;

                    case SecurityPackageType.NetLogon:
                        context.AuthenticationType = RpceAuthenticationType.RPC_C_AUTHN_NETLOGON;
                        break;

                    default:
                        throw new NotSupportedException("Specified SSPI is not supported.");
                }
            }

            context.SecurityContext = securityContext;
            context.AuthenticationLevel = authenticationLevel;
            context.AuthenticationContextId = authenticationContextId;
        }
        /// <summary>
        /// RPC bind over named pipe, using well-known endpoint "\PIPE\NETLOGON".
        /// </summary>
        /// <param name="serverName">NRPC server machine name.</param>
        /// <param name="transportCredential">
        /// If connect by SMB/SMB2, it's the security credential 
        /// used by under layer transport (SMB/SMB2). 
        /// If connect by TCP, this parameter is ignored.
        /// </param>
        /// <param name="securityContext">
        /// Security provider for RPC. 
        /// Set the value to null to disable authentication.
        /// </param>
        /// <param name="timeout">Timeout for bind and all future requests.</param>
        /// <exception cref="ArgumentNullException">
        /// Thrown when serverName is null.
        /// </exception>
        public void BindOverNamedPipe(
            string serverName,
            AccountCredential transportCredential,
            ClientSecurityContext securityContext,
            TimeSpan timeout)
        {
            if (serverName == null)
            {
                throw new ArgumentNullException("serverName");
            }

            nrpcSecurityContext = securityContext as NrpcClientSecurityContext;
            if (nrpcSecurityContext != null)
            {
                context = nrpcSecurityContext.nrpc.context;
            }

            rpc.Bind(
                RpceUtility.RPC_OVER_NAMED_PIPE_PROTOCOL_SEQUENCE,
                serverName,
                NrpcUtility.NETLOGON_RPC_OVER_NP_WELLKNOWN_ENDPOINT,
                transportCredential,
                securityContext,
                securityContext == null
                    ? RpceAuthenticationLevel.RPC_C_AUTHN_LEVEL_NONE
                    : (context.SealSecureChannel
                        ? RpceAuthenticationLevel.RPC_C_AUTHN_LEVEL_PKT_PRIVACY
                        : RpceAuthenticationLevel.RPC_C_AUTHN_LEVEL_PKT_INTEGRITY),
                timeout);

            context.PrimaryName = serverName;
            NrpcRpcAdapter nrpcRpcAdapter = rpc as NrpcRpcAdapter;
            if (nrpcRpcAdapter != null)
            {
                context.rpceTransportContext = nrpcRpcAdapter.rpceClientTransport.Context;
            }
        }
 /// <summary>
 /// Connect and bind to a RPCE remote host.
 /// </summary>
 /// <param name="protocolSequence">
 /// A protocol sequence.<para/>
 /// Support ncacn_ip_tcp and ncacn_np only.
 /// </param>
 /// <param name="networkAddress">
 /// A network address of RPCE remote host.
 /// </param>
 /// <param name="endpoint">
 /// An endpoint that its format and content 
 /// are associated with the protocol sequence.
 /// </param>
 /// <param name="transportCredential">
 /// If connect by SMB/SMB2, it's the security credential 
 /// used by underlayer transport (SMB/SMB2). 
 /// If connect by TCP, this parameter is ignored.
 /// </param>
 /// <param name="interfaceId">
 /// A Guid of interface_id that is binding to.
 /// </param>
 /// <param name="interfaceMajorVersion">
 /// interface_major_ver that is binding to.
 /// </param>
 /// <param name="interfaceMinorVersion">
 /// interface_minor_ver that is binding to.
 /// </param>
 /// <param name="securityContext">
 /// A security provider. If setting to null, indicate the default authentication type NTLM is selected.
 /// </param>
 /// <param name="authenticationLevel">
 /// An authentication level.
 /// </param>
 /// <param name="supportsHeaderSign">
 /// Indicates whether client supports header sign or not.
 /// </param>
 /// <param name="timeout">
 /// Timeout period.
 /// </param>
 /// <exception cref="ArgumentNullException">
 /// Thrown when protSeq, networkAddr or endpoint is null.
 /// </exception>
 /// <exception cref="InvalidOperationException">
 /// Thrown when receive error from server.
 /// </exception>
 public virtual void Bind(
     string protocolSequence,
     string networkAddress,
     string endpoint,
     AccountCredential transportCredential,
     Guid interfaceId,
     ushort interfaceMajorVersion,
     ushort interfaceMinorVersion,
     ClientSecurityContext securityContext,
     RpceAuthenticationLevel authenticationLevel,
     bool supportsHeaderSign,
     TimeSpan timeout)
 {
     Bind(protocolSequence, networkAddress, endpoint, transportCredential, interfaceId, interfaceMajorVersion, interfaceMinorVersion,
         securityContext, securityContext, authenticationLevel, supportsHeaderSign, timeout);
 }
        /// <summary>
        /// Connect and bind to a RPCE remote host.
        /// </summary>
        /// <param name="protocolSequence">
        /// A protocol sequence.<para/>
        /// Support ncacn_ip_tcp and ncacn_np only.
        /// </param>
        /// <param name="networkAddress">
        /// A network address of RPCE remote host.
        /// </param>
        /// <param name="endpoint">
        /// An endpoint that its format and content 
        /// are associated with the protocol sequence.
        /// </param>
        /// <param name="transportCredential">
        /// If connect by SMB/SMB2, it's the security credential 
        /// used by underlayer transport (SMB/SMB2). 
        /// If connect by TCP, this parameter is ignored.
        /// </param>
        /// <param name="interfaceId">
        /// A Guid of interface_id that is binding to.
        /// </param>
        /// <param name="interfaceMajorVersion">
        /// interface_major_ver that is binding to.
        /// </param>
        /// <param name="interfaceMinorVersion">
        /// interface_minor_ver that is binding to.
        /// </param>
        /// <param name="securityContext">
        /// A security provider. If setting to null, indicate the default authentication type NTLM is selected.
        /// </param>
        /// <param name="connectSecurityContext">
        /// A security provider for connect authentication. If setting to null, indicate the default authentication type NTLM is selected.
        /// </param>
        /// <param name="authenticationLevel">
        /// An authentication level.
        /// </param>
        /// <param name="supportsHeaderSign">
        /// Indicates whether client supports header sign or not.
        /// </param>
        /// <param name="timeout">
        /// Timeout period.
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// Thrown when protSeq, networkAddr or endpoint is null.
        /// </exception>
        /// <exception cref="InvalidOperationException">
        /// Thrown when receive error from server.
        /// </exception>
        public virtual void Bind(
            string protocolSequence,
            string networkAddress,
            string endpoint,
            AccountCredential transportCredential,
            Guid interfaceId,
            ushort interfaceMajorVersion,
            ushort interfaceMinorVersion,
            ClientSecurityContext securityContext,
            ClientSecurityContext connectSecurityContext,
            RpceAuthenticationLevel authenticationLevel,
            bool supportsHeaderSign,
            TimeSpan timeout)
        {
            if (protocolSequence == null)
            {
                throw new ArgumentNullException("protocolSequence");
            }
            if (networkAddress == null)
            {
                throw new ArgumentNullException("networkAddress");
            }
            if (endpoint == null)
            {
                throw new ArgumentNullException("endpoint");
            }

            // RPC over named-pipe does not support asynchronous call in this library.
            // http://msdn.microsoft.com/en-us/library/windows/desktop/aa373551(v=vs.85).aspx
            rpceClient.Context.IsAsynchronous = (string.Compare(protocolSequence, RpceUtility.RPC_OVER_NAMED_PIPE_PROTOCOL_SEQUENCE, true) != 0);

            rpceClient.Connect(protocolSequence, networkAddress, endpoint, transportCredential, timeout,
                connectSecurityContext == null ? SecurityPackageType.Ntlm : connectSecurityContext.PackageType);

            rpceClient.SetAuthInfo(securityContext, authenticationLevel, rpceClient.Context.AuthenticationContextId);

            RpceCoBindPdu bindPdu = rpceClient.CreateCoBindPdu(
                //read from context, donot hardcode.
                rpceClient.Context.RpcVersionMinor, // default is rpc vers 5.0
                supportsHeaderSign ? RpceCoPfcFlags.PFC_SUPPORT_HEADER_SIGN : RpceCoPfcFlags.None,
                rpceClient.ComputeNextCallId(), // call id, default is 1
                rpceClient.Context.MaxTransmitFragmentSize, // max xmit frag
                rpceClient.Context.MaxReceiveFragmentSize, // max recv frag
                rpceClient.Context.AssociateGroupId, // assoc group id, default 0
                interfaceId,
                interfaceMajorVersion,
                interfaceMinorVersion,
                rpceClient.Context.NdrVersion, // default is NDR
                (rpceClient.Context.BindTimeFeatureNegotiationBitmask != RpceBindTimeFeatureNegotiationBitmask.None) // default is None
                    ? (RpceBindTimeFeatureNegotiationBitmask?)rpceClient.Context.BindTimeFeatureNegotiationBitmask
                    : null);

            FragmentAndSendPdu(bindPdu);

            RpcePdu receivedPdu = ReceiveAndReassemblePdu(timeout);

            if (receivedPdu is RpceCoBindAckPdu)
            {
                if (rpceClient.Context.NdrVersion == RpceNdrVersion.None)
                {
                    throw new InvalidOperationException("Neither NDR nor NDR64 is supported.");
                }
            }
            else
            {
                RpceCoBindNakPdu bindNakPdu = receivedPdu as RpceCoBindNakPdu;
                if (bindNakPdu != null)
                {
                    throw new InvalidOperationException(bindNakPdu.provider_reject_reason.ToString());
                }
                else
                {
                    throw new InvalidOperationException(
                        string.Format("Unexpected packet type received - {0}.",
                        receivedPdu.GetType().Name));
                }
            }

            while (rpceClient.Context.SecurityContext != null
                && rpceClient.Context.SecurityContext.NeedContinueProcessing)
            {
                RpceCoAlterContextPdu alterContextPdu = rpceClient.CreateCoAlterContextPdu();
                FragmentAndSendPdu(alterContextPdu);
                receivedPdu = ReceiveAndReassemblePdu(timeout);

                RpceCoFaultPdu faultPdu = receivedPdu as RpceCoFaultPdu;
                if (faultPdu != null)
                {
                    throw new InvalidOperationException(faultPdu.status.ToString());
                }

                if (!(receivedPdu is RpceCoAlterContextRespPdu))
                {
                    throw new InvalidOperationException("Expect alter_context_pdu, but received others.");
                }
            }

            if (rpceClient.Context.SecurityContext != null
                && rpceClient.Context.SecurityContext.Token != null
                && rpceClient.Context.SecurityContext.Token.Length != 0)
            {
                RpceCoAuth3Pdu auth3Pdu = rpceClient.CreateCoAuth3Pdu();
                FragmentAndSendPdu(auth3Pdu);
                // no expected response from server
                rpceClient.Context.OutstandingCalls.Remove(auth3Pdu.call_id);
            }

            if (rpceClient.Context.IsAsynchronous)
            {
                // Start the receiving thread to receive the response from server.
                receiveThread = new Thread(new ThreadStart(EventLoop));
                receiveThread.IsBackground = true;
                receiveThread.Start();
            }
        }
        /// <summary>
        /// RPC bind to interface, using specified endpoint and authenticate provider.
        /// </summary>
        /// <param name="protocolSequence">
        /// RPC protocol sequence.
        /// </param>
        /// <param name="networkAddress">
        /// RPC network address.
        /// </param>
        /// <param name="endpoint">
        /// RPC endpoint.
        /// </param>
        /// <param name="transportCred">
        /// Credential to bind SWN server
        /// </param>
        /// <param name="secContext">
        /// RPC security provider.
        /// </param>
        /// <param name="authLevel">
        /// RPC authentication level.
        /// </param>
        /// <param name="timeout">
        /// Timeout
        /// </param>
        /// <exception cref="InvalidOperationException">
        /// Thrown when there's existing connection.
        /// </exception>
        private bool RpcBind(
            string protocolSequence,
            string networkAddress,
            string endpoint,
            AccountCredential transportCred,
            ClientSecurityContext secContext,
            RpceAuthenticationLevel authLevel,
            TimeSpan timeout)
        {
            if (rpceClientTransport != null)
            {
                throw new InvalidOperationException("Connection has been established");
            }

            rpceTimeout = timeout;
            rpceClientTransport = new RpceClientTransport();

            try
            {
                rpceClientTransport.Bind(
                    protocolSequence,
                    networkAddress,
                    endpoint,
                    transportCred,
                    SwnUtility.SWN_INTERFACE_UUID,
                    SwnUtility.SWN_INTERFACE_MAJOR_VERSION,
                    SwnUtility.SWN_INTERFACE_MINOR_VERSION,
                    secContext,
                    authLevel,
                    true,
                    rpceTimeout);
            }
            catch (Exception)
            {
                rpceClientTransport.Dispose();
                rpceClientTransport = null;
                throw;
            }

            return true;
        }
        /// <summary>
        /// Bind SWN to the RPC server.
        /// </summary>
        /// <param name="serverName">SWN server name to bind</param>
        /// <param name="bindCred">Credential to bind SWN server</param>
        /// <param name="secContext">Security provider for RPC</param>
        /// <param name="timeout">Timeout for bind and requests</param>
        /// <param name="authLevel">RPCE authentication level</param>
        /// <returns>Return true if success, or false for fail</returns>
        ///<exception cref="ArgumentNullException">
        /// Thrown when serverName is null or empty.
        /// </exception>
        public bool SwnBind(
            string serverName,
            AccountCredential bindCred,
            ClientSecurityContext secContext,
            RpceAuthenticationLevel authLevel,
            TimeSpan timeout)
        {
            if (string.IsNullOrEmpty(serverName))
            {
                throw new ArgumentNullException("serverName");
            }

            //Query endpoint on SWN server
            ushort[] endpoints = SwnUtility.QueryEndpoints(serverName);

            bool retVal = RpcBind(
                RpceUtility.RPC_OVER_TCPIP_PROTOCOL_SEQUENCE,
                serverName,
                endpoints[0].ToString(),
                bindCred,
                secContext,
                authLevel,
                timeout);

            rpceTimeout = timeout;

            return retVal;
        }