/// <summary>
        /// Closes the specified connections to the remote host and releases acquired resources.
        /// </summary>
        /// <param name="hostInformation">Host information.</param>
        /// <param name="genuineConnectionType">What kind of connections will be affected by this operation.</param>
        /// <param name="reason">The reason of resource releasing.</param>
        public override void ReleaseConnections(HostInformation hostInformation, GenuineConnectionType genuineConnectionType, Exception reason)
        {
            BinaryLogWriter binaryLogWriter = this.ITransportContext.BinaryLogWriter;
            ArrayList connectionsToClose = new ArrayList();

            using (new WriterAutoLocker(this._disposeLock))
            {
                if (this._disposed)
                    return ;

                // LOG:
                if ( binaryLogWriter != null && binaryLogWriter[LogCategory.Connection] > 0 )
                {
                    binaryLogWriter.WriteEvent(LogCategory.Connection, "HttpServerConnectionManager.ReleaseConnections",
                        LogMessageType.ReleaseConnections, reason, null, hostInformation, null,
                        GenuineUtility.CurrentThreadId, Thread.CurrentThread.Name,
                        null, null, -1, 0, 0, 0, Enum.Format(typeof(GenuineConnectionType), genuineConnectionType, "g"), null, null, null,
                        "Connections \"{0}\" will be terminated.", Enum.Format(typeof(GenuineConnectionType), genuineConnectionType, "g"), null);
                }

                // persistent
                if ( (genuineConnectionType & GenuineConnectionType.Persistent) != 0 )
                {
                    // persistent
                    ReleaseConnections_Parameters releaseConnections_Parameters = new ReleaseConnections_Parameters();
                    releaseConnections_Parameters.FailedConnections = connectionsToClose;
                    releaseConnections_Parameters.HostInformation = hostInformation;

                    this._persistent.InspectAllConnections(this._releaseConnections_InspectPersistentConnections, releaseConnections_Parameters);
                }

                // close connections
                foreach (HttpServerConnection nextHttpServerConnection in connectionsToClose)
                {
                    lock (nextHttpServerConnection.Listener_Lock)
                    {
                        // LOG:
                        if ( binaryLogWriter != null && binaryLogWriter[LogCategory.Connection] > 0 )
                        {
                            binaryLogWriter.WriteEvent(LogCategory.Connection, "HttpClientConnectionManager.ReleaseConnections",
                                LogMessageType.ConnectionShuttingDown, reason, null, nextHttpServerConnection.Remote, null,
                                GenuineUtility.CurrentThreadId, Thread.CurrentThread.Name, null, null,
                                nextHttpServerConnection.DbgConnectionId, 0, 0, 0, null, null, null, null,
                                "The connection is being shut down manually.");
                        }

                        nextHttpServerConnection.SignalState(GenuineEventType.GeneralConnectionClosed, reason, null);

                        this._persistent.Remove(nextHttpServerConnection.Remote.Uri, nextHttpServerConnection.ConnectionName);
                        if (nextHttpServerConnection.Listener != null)
                            this.Pool_SendThroughListener(nextHttpServerConnection, HttpPacketType.ClosedManually);
                    }
                }
            }
        }
        /// <summary>
        /// Closes the specified connections to the remote host and releases acquired resources.
        /// </summary>
        /// <param name="hostInformation">Host information or a null reference to close all connection according to specified types.</param>
        /// <param name="genuineConnectionType">What kind of connections will be affected by this operation.</param>
        /// <param name="reason">Reason of resource releasing.</param>
        public override void ReleaseConnections(HostInformation hostInformation, GenuineConnectionType genuineConnectionType, Exception reason)
        {
            TcpSocketInfo tcpSocketInfo = null;
            BinaryLogWriter binaryLogWriter = this.ITransportContext.BinaryLogWriter;

            // LOG:
            if ( binaryLogWriter != null && binaryLogWriter[LogCategory.Connection] > 0 )
            {
                binaryLogWriter.WriteEvent(LogCategory.Connection, "TcpConnectionManager.ReleaseConnections",
                    LogMessageType.ReleaseConnections, reason, null, hostInformation, null,
                    GenuineUtility.CurrentThreadId, Thread.CurrentThread.Name,
                    null, null, -1, 0, 0, 0, Enum.Format(typeof(GenuineConnectionType), genuineConnectionType, "g"), null, null, null,
                    "Connections \"{0}\" are terminated.", Enum.Format(typeof(GenuineConnectionType), genuineConnectionType, "g"), null);
            }

            // go through all connections and close such of them that fall under the specified category
            ArrayList failedConnections = new ArrayList();

            // persistent
            ReleaseConnections_Parameters releaseConnections_Parameters = new ReleaseConnections_Parameters();
            releaseConnections_Parameters.FailedConnections = failedConnections;
            releaseConnections_Parameters.HostInformation = hostInformation;
            releaseConnections_Parameters.GenuineConnectionType = genuineConnectionType;

            this._persistent.InspectAllConnections(this._releaseConnections_InspectPersistentConnections, releaseConnections_Parameters);

            // named
            if ( (genuineConnectionType & GenuineConnectionType.Persistent) != 0 || (genuineConnectionType & GenuineConnectionType.Named) != 0)
                lock (this._named)
                {
                    foreach (DictionaryEntry entry in this._named)
                    {
                        tcpSocketInfo = (TcpSocketInfo) entry.Value;
                        if (hostInformation != null && tcpSocketInfo.Remote != hostInformation)
                            continue;
                        if ( (genuineConnectionType & tcpSocketInfo.GenuineConnectionType) == 0)
                            continue;

                        failedConnections.Add(tcpSocketInfo);
                    }
                }

            // invocation
            if ( (genuineConnectionType & GenuineConnectionType.Invocation) != 0)
            {
                lock (this._invocation.SyncRoot)
                    foreach (DictionaryEntry entry in this._invocation)
                    {
                        ArrayList connections = (ArrayList) entry.Value;
                        lock (connections)
                        {
                            foreach (TcpSocketInfo nextTcpSocketInfo in connections)
                            {
                                if (hostInformation != null && nextTcpSocketInfo.Remote != hostInformation)
                                    continue;

                                failedConnections.Add(nextTcpSocketInfo);
                            }
                        }
                    }

                lock (this._knownInvocationConnections.SyncRoot)
                    foreach (DictionaryEntry entry in this._knownInvocationConnections)
                    {
                        tcpSocketInfo = (TcpSocketInfo) entry.Value;
                        if (hostInformation != null && tcpSocketInfo.Remote != hostInformation)
                            continue;

                        failedConnections.Add(tcpSocketInfo);
                    }
            }

            // close connections
            foreach (TcpSocketInfo failedTcpSocketInfo in failedConnections)
            {
                // LOG:
                if ( binaryLogWriter != null && binaryLogWriter[LogCategory.Security] > 0 )
                {
                    binaryLogWriter.WriteEvent(LogCategory.Connection, "TcpConnectionManager.ReleaseConnections",
                        LogMessageType.ConnectionShuttingDown, reason, null, failedTcpSocketInfo.Remote, null,
                        GenuineUtility.CurrentThreadId, Thread.CurrentThread.Name,
                        null, null, failedTcpSocketInfo.DbgConnectionId, 0, 0, 0, null, null, null, null,
                        "The connection is terminated.");
                }

                this.SocketFailed(GenuineExceptions.Get_Channel_ConnectionShutDown(reason), failedTcpSocketInfo);

                if (failedTcpSocketInfo.GenuineConnectionType == GenuineConnectionType.Persistent)
                    failedTcpSocketInfo.SignalState(GenuineEventType.GeneralConnectionClosed, reason, null);
            }
        }
        /// <summary>
        /// Closes the specified connections to the remote host and releases acquired resources.
        /// </summary>
        /// <param name="hostInformation">Host information.</param>
        /// <param name="genuineConnectionType">What kind of connections will be affected by this operation.</param>
        /// <param name="reason">Reason of resource releasing.</param>
        public override void ReleaseConnections(HostInformation hostInformation, GenuineConnectionType genuineConnectionType, Exception reason)
        {
            BinaryLogWriter binaryLogWriter = this.ITransportContext.BinaryLogWriter;
            ArrayList connectionsToClose = new ArrayList();

            // LOG:
            if ( binaryLogWriter != null && binaryLogWriter[LogCategory.Connection] > 0 )
            {
                binaryLogWriter.WriteEvent(LogCategory.Connection, "XHttpConnectionManager.ReleaseConnections",
                    LogMessageType.ReleaseConnections, reason, null, hostInformation, null,
                    GenuineUtility.CurrentThreadId, Thread.CurrentThread.Name,
                    null, null, -1, 0, 0, 0, Enum.Format(typeof(GenuineConnectionType), genuineConnectionType, "g"), null, null, null,
                    "Connections \"{0}\" are terminated.", Enum.Format(typeof(GenuineConnectionType), genuineConnectionType, "g"), null);
            }

            using (new WriterAutoLocker(this._disposeLock))
            {
                // persistent
                if ( (genuineConnectionType & GenuineConnectionType.Persistent) != 0 )
                {
                    ReleaseConnections_Parameters releaseConnections_Parameters = new ReleaseConnections_Parameters();
                    releaseConnections_Parameters.FailedConnections = connectionsToClose;
                    releaseConnections_Parameters.HostInformation = hostInformation;

                    this._persistent.InspectAllConnections(this._releaseConnections_InspectPersistentConnections, releaseConnections_Parameters);
                }
            }

            // close connections
            foreach (XHttpConnection nextXHttpConnection in connectionsToClose)
            {
                // LOG:
                if ( binaryLogWriter != null && binaryLogWriter[LogCategory.Connection] > 0 )
                {
                    binaryLogWriter.WriteEvent(LogCategory.Connection, "XHttpConnectionManager.ReleaseConnections",
                        LogMessageType.ConnectionShuttingDown, reason, null, hostInformation, null,
                        GenuineUtility.CurrentThreadId, Thread.CurrentThread.Name,
                        null, null, nextXHttpConnection.DbgConnectionId, 0, 0, 0, Enum.Format(typeof(GenuineConnectionType), genuineConnectionType, "g"), null, null, null,
                        "Connection is terminated.");
                }

                this.ConnectionFailed(GenuineExceptions.Get_Channel_ConnectionShutDown(reason), nextXHttpConnection, null);

                if (nextXHttpConnection.GenuineConnectionType == GenuineConnectionType.Persistent)
                    nextXHttpConnection.SignalState(GenuineEventType.GeneralConnectionClosed, reason, null);
            }
        }