private void RunPipeServer()
        {
            var pipeServer = new NamedPipeServerStream("testpipe", PipeDirection.InOut, 1,
                                     PipeTransmissionMode.Byte, PipeOptions.Asynchronous);
            try
            {
                pipeServer.BeginWaitForConnection(asyncResult =>
                {
                    using (var pipe = (NamedPipeServerStream)asyncResult.AsyncState)
                    {
                        try
                        {
                            pipe.EndWaitForConnection(asyncResult);

                            ChannelStream stream = new ChannelStream(pipe);
                            string message = stream.Receive();
                            Console.WriteLine(message);
                            stream.Send("This is server, received your message, client.");
                            pipe.WaitForPipeDrain();

                            pipe.Disconnect();
                            pipe.Close();
                        }
                        catch(Exception e)
                        {
                            Console.WriteLine("EndWaitForConnection: {0}", e.Message);
                        }

                        RunPipeServer();
                    }

                },  pipeServer);
            }
            catch (Exception ee)
            {
                Console.WriteLine("Exception: {0}", ee.Message);
            }
        }
        private void RunPipeClient()
        {
            NamedPipeClientStream client = new NamedPipeClientStream(".", "testpipe", PipeDirection.InOut,
                PipeOptions.Asynchronous, TokenImpersonationLevel.Impersonation);

            client.Connect();

            ChannelStream stream = new ChannelStream(client);

            stream.Send("Hello Server, this is client !");

            string response = stream.Receive();

            Console.WriteLine(response);

            client.Close();
        }
        private void ReadWriteMessage(NamedPipeServerStream pipeStream)
        {
            try
            {
                ChannelStream channelStream = new ChannelStream(pipeStream);

                string inMessage = channelStream.Receive();

                if (this.ReceiveResponseEventHandler != null)
                {
                    try
                    {
                        string response = this.ReceiveResponseEventHandler.Invoke(inMessage);

                        if (!string.IsNullOrWhiteSpace(response))
                        {
                            channelStream.Send(response);

                            pipeStream.WaitForPipeDrain();
                        }
                    }
                    catch { }
                }
            }
            catch { }
        }
        private string ReadWriteMessage(NamedPipeClientStream pipeClient, string message)
        {
            try
            {
                ChannelStream channelStream = new ChannelStream(pipeClient);

                channelStream.Send(message);

                pipeClient.WaitForPipeDrain();

                string response = channelStream.Receive();

                pipeClient.Close();

                return response;
            }
            catch
            {
                return null;
            }
        }