/// <summary>
        /// Listens to client requests and put them to the queue from where the working thread takes them
        /// and notifies the call-backs the pipe input channel.
        /// </summary>
        private void DoListening()
        {
            using (EneterTrace.Entering())
            {
                try
                {
                    // Reconnect the server pipe in the loop.
                    // If the client is not available the thread will wait forewer on WaitForConnection().
                    // When the listening is stopped then the thread is correctly released.
                    while (!myStopListeningRequestFlag)
                    {
                        myPipeServer.WaitForConnection();

                        DynamicStream aDynamicStream = null;
                        try
                        {
                            aDynamicStream = new DynamicStream();

                            // Start thread for reading received messages.
                            EneterThreadPool.QueueUserWorkItem(() =>
                            {
                                while (!myStopListeningRequestFlag && myPipeServer.IsConnected)
                                {
                                    // Read the whole message.
                                    try
                                    {
                                        myMessageHandler(aDynamicStream);
                                    }
                                    catch (Exception err)
                                    {
                                        EneterTrace.Warning(TracedObject + ErrorHandler.DetectedException, err);
                                    }
                                }

                                aDynamicStream.Close();
                            });

                            // Write incoming messages to the dynamic stream from where the reading thread will notify them.
                            if (!myStopListeningRequestFlag && myPipeServer.IsConnected)
                            {
                                // Read the whole message.
                                try
                                {
                                    StreamUtil.ReadToEndWithoutCopying(myPipeServer, aDynamicStream);
                                }
                                catch (Exception err)
                                {
                                    EneterTrace.Warning(TracedObject + ErrorHandler.DetectedException, err);
                                }
                            }
                        }
                        finally
                        {
                            if (aDynamicStream != null)
                            {
                                // Note: Do not close the dynamic stream here!
                                //       There can be data read before the closing and the client can still finish to read them.
                                //aDynamicStream.Close();

                                // Unblock the reading thread that can be waiting for messages.
                                aDynamicStream.IsBlockingMode = false;
                            }
                        }

                        // Disconnect the client.
                        if (!myStopListeningRequestFlag)
                        {
                            myPipeServer.Disconnect();
                        }
                    }
                }
                catch (Exception err)
                {
                    // if the error is not caused by closed communication.
                    if (!myStopListeningRequestFlag)
                    {
                        EneterTrace.Error(TracedObject + ErrorHandler.FailedInListeningLoop, err);
                    }
                }
            }
        }