/// <summary>
        /// Releases all resources related to the specified connection.
        /// </summary>
        /// <param name="exception">The reason.</param>
        /// <param name="sharedMemoryConnection">The connection.</param>
        private void ConnectionFailed(Exception exception, SharedMemoryConnection sharedMemoryConnection)
        {
            BinaryLogWriter binaryLogWriter = this.ITransportContext.BinaryLogWriter;

            try
            {
                sharedMemoryConnection.IsValid = false;

                // LOG:
                if (binaryLogWriter != null && binaryLogWriter[LogCategory.Connection] > 0)
                {
                    binaryLogWriter.WriteEvent(LogCategory.Connection, "SharedMemoryConnectionManager.ConnectionFailed",
                                               LogMessageType.ConnectionFailed, exception, null, sharedMemoryConnection.Remote, null,
                                               GenuineUtility.CurrentThreadId, Thread.CurrentThread.Name,
                                               null, null,
                                               sharedMemoryConnection.DbgConnectionId, 0, 0, 0, null, null, null, null,
                                               "Connection has failed.");
                }

                // unregister the connection
                if (sharedMemoryConnection.Remote.GenuinePersistentConnectionState == GenuinePersistentConnectionState.Accepted)
                {
                    this._persistent.Remove(sharedMemoryConnection.Remote.Uri);
                }
                else
                {
                    this._persistent.Remove(sharedMemoryConnection.Remote.Url);
                }

                // release all resources
                this.ITransportContext.KnownHosts.ReleaseHostResources(sharedMemoryConnection.Remote, exception);
                sharedMemoryConnection.ReleaseUnmanagedResources();
                sharedMemoryConnection.SignalState(GenuineEventType.GeneralConnectionClosed, exception, null);
            }
            catch (Exception ex)
            {
                // LOG:
                if (binaryLogWriter != null)
                {
                    binaryLogWriter.WriteImplementationWarningEvent("SharedMemoryConnectionManager.ConnectionFailed",
                                                                    LogMessageType.CriticalError, ex,
                                                                    GenuineUtility.CurrentThreadId, Thread.CurrentThread.Name,
                                                                    "Unexpected exception inside the SharedMemoryClientConnectionManager.ConnectionFailed method. Most likely, something must be fixed.");
                }
            }
        }
        /// <summary>
        /// Releases all resources related to the specified connection.
        /// </summary>
        /// <param name="exception">The reason.</param>
        /// <param name="sharedMemoryConnection">The connection.</param>
        private void ConnectionFailed(Exception exception, SharedMemoryConnection sharedMemoryConnection)
        {
            BinaryLogWriter binaryLogWriter = this.ITransportContext.BinaryLogWriter;

            try
            {
                sharedMemoryConnection.IsValid = false;

                // LOG:
                if ( binaryLogWriter != null && binaryLogWriter[LogCategory.Connection] > 0 )
                {
                    binaryLogWriter.WriteEvent(LogCategory.Connection, "SharedMemoryConnectionManager.ConnectionFailed",
                        LogMessageType.ConnectionFailed, exception, null, sharedMemoryConnection.Remote, null,
                        GenuineUtility.CurrentThreadId, Thread.CurrentThread.Name,
                        null, null,
                        sharedMemoryConnection.DbgConnectionId, 0, 0, 0, null, null, null, null,
                        "Connection has failed.");
                }

                // unregister the connection
                if (sharedMemoryConnection.Remote.GenuinePersistentConnectionState == GenuinePersistentConnectionState.Accepted)
                    this._persistent.Remove(sharedMemoryConnection.Remote.Uri);
                else
                    this._persistent.Remove(sharedMemoryConnection.Remote.Url);

                // release all resources
                this.ITransportContext.KnownHosts.ReleaseHostResources(sharedMemoryConnection.Remote, exception);
                sharedMemoryConnection.ReleaseUnmanagedResources();
                sharedMemoryConnection.SignalState(GenuineEventType.GeneralConnectionClosed, exception, null);
            }
            catch(Exception ex)
            {
                // LOG:
                if ( binaryLogWriter != null)
                {
                    binaryLogWriter.WriteImplementationWarningEvent("SharedMemoryConnectionManager.ConnectionFailed",
                        LogMessageType.CriticalError, ex,
                        GenuineUtility.CurrentThreadId, Thread.CurrentThread.Name,
                        "Unexpected exception inside the SharedMemoryClientConnectionManager.ConnectionFailed method. Most likely, something must be fixed.");
                }
            }
        }
        /// <summary>
        /// Starts listening to the specified end point and accepting incoming connections.
        /// </summary>
        /// <param name="endPoint">The end point.</param>
        public override void StartListening(object endPoint)
        {
            BinaryLogWriter binaryLogWriter = this.ITransportContext.BinaryLogWriter;

            if (this._smAcceptConnectionClosure != null)
                throw GenuineExceptions.Get_Server_EndPointIsAlreadyBeingListenedTo(this._smAcceptConnectionClosure.ShareName);
            SharedMemoryConnection sharedMemoryConnection = null;

            using (new ReaderAutoLocker(this._disposeLock))
            {
                if (this._disposed)
                    throw OperationException.WrapException(this._disposeReason);
            }

            string shareName = endPoint as string;
            if (shareName == null || shareName.Length <= 0 || ! shareName.StartsWith("gshmem"))
                throw GenuineExceptions.Get_Server_IncorrectAddressToListen(shareName);

            try
            {
                sharedMemoryConnection = new SharedMemoryConnection(this.ITransportContext, shareName, true, true);

                this._smAcceptConnectionClosure = new SMAcceptConnectionClosure(this.ITransportContext, sharedMemoryConnection, this, shareName);
                this.ITransportContext.IGenuineEventProvider.Fire(new GenuineEventArgs(GenuineEventType.GeneralListenerStarted, null, this.Local, endPoint));

                Thread thread = new Thread(new ThreadStart(this._smAcceptConnectionClosure.AcceptConnections));
                thread.IsBackground = true;
                thread.Start();

                // LOG:
                if ( binaryLogWriter != null && binaryLogWriter[LogCategory.Connection] > 0 )
                {
                    binaryLogWriter.WriteEvent(LogCategory.AcceptingConnection, "SharedMemoryConnectionManager.StartListening",
                        LogMessageType.ListeningStarted, null, null, null, null,
                        GenuineUtility.CurrentThreadId, Thread.CurrentThread.Name,
                        null, null, -1,
                        0, 0, 0, shareName, null, null, null,
                        "\"{0}\" is now listened.", shareName);
                }
            }
            catch(Exception ex)
            {
                // LOG:
                if ( binaryLogWriter != null && binaryLogWriter[LogCategory.Connection] > 0 )
                {
                    binaryLogWriter.WriteEvent(LogCategory.AcceptingConnection, "SharedMemoryConnectionManager.StartListening",
                        LogMessageType.ListeningStarted, ex, null, null, null,
                        GenuineUtility.CurrentThreadId, Thread.CurrentThread.Name,
                        null, null, -1,
                        0, 0, 0, shareName, null, null, null,
                        "Listening to \"{0}\" cannot be started.", shareName);
                }

                if (sharedMemoryConnection != null)
                    sharedMemoryConnection.ReleaseUnmanagedResources();
                throw;
            }
        }
        /// <summary>
        /// Starts listening to the specified end point and accepting incoming connections.
        /// </summary>
        /// <param name="endPoint">The end point.</param>
        public override void StartListening(object endPoint)
        {
            BinaryLogWriter binaryLogWriter = this.ITransportContext.BinaryLogWriter;

            if (this._smAcceptConnectionClosure != null)
            {
                throw GenuineExceptions.Get_Server_EndPointIsAlreadyBeingListenedTo(this._smAcceptConnectionClosure.ShareName);
            }
            SharedMemoryConnection sharedMemoryConnection = null;

            using (new ReaderAutoLocker(this._disposeLock))
            {
                if (this._disposed)
                {
                    throw OperationException.WrapException(this._disposeReason);
                }
            }

            string shareName = endPoint as string;

            if (shareName == null || shareName.Length <= 0 || !shareName.StartsWith("gshmem"))
            {
                throw GenuineExceptions.Get_Server_IncorrectAddressToListen(shareName);
            }

            try
            {
                sharedMemoryConnection = new SharedMemoryConnection(this.ITransportContext, shareName, true, true);

                this._smAcceptConnectionClosure = new SMAcceptConnectionClosure(this.ITransportContext, sharedMemoryConnection, this, shareName);
                this.ITransportContext.IGenuineEventProvider.Fire(new GenuineEventArgs(GenuineEventType.GeneralListenerStarted, null, this.Local, endPoint));

                Thread thread = new Thread(new ThreadStart(this._smAcceptConnectionClosure.AcceptConnections));
                thread.IsBackground = true;
                thread.Start();

                // LOG:
                if (binaryLogWriter != null && binaryLogWriter[LogCategory.Connection] > 0)
                {
                    binaryLogWriter.WriteEvent(LogCategory.AcceptingConnection, "SharedMemoryConnectionManager.StartListening",
                                               LogMessageType.ListeningStarted, null, null, null, null,
                                               GenuineUtility.CurrentThreadId, Thread.CurrentThread.Name,
                                               null, null, -1,
                                               0, 0, 0, shareName, null, null, null,
                                               "\"{0}\" is now listened.", shareName);
                }
            }
            catch (Exception ex)
            {
                // LOG:
                if (binaryLogWriter != null && binaryLogWriter[LogCategory.Connection] > 0)
                {
                    binaryLogWriter.WriteEvent(LogCategory.AcceptingConnection, "SharedMemoryConnectionManager.StartListening",
                                               LogMessageType.ListeningStarted, ex, null, null, null,
                                               GenuineUtility.CurrentThreadId, Thread.CurrentThread.Name,
                                               null, null, -1,
                                               0, 0, 0, shareName, null, null, null,
                                               "Listening to \"{0}\" cannot be started.", shareName);
                }

                if (sharedMemoryConnection != null)
                {
                    sharedMemoryConnection.ReleaseUnmanagedResources();
                }
                throw;
            }
        }