title: Determine if NamedPipeClientStream is connected
body: <p>I've problem with process communication using PipeConnection</p>
  
  <p>My idea is:<br /></p>
  
  <blockquote>
    <p>A - process A <br />
    B - process B<br />
    1. A(pile client) wants to send message to B(pipe server)<br />
    2. B reads message and disconects.<br /></p>
    
    <blockquote>
      <p>2.1 If B needs to ansver<br />
      2.2 B(pipe client) connects to A(pile server)
      2.3 A reads message and disconects.<br /></p>
    </blockquote>
  </blockquote>
  
  <p>The problem appears if A sends message to B and than imedlitley wants to send another message again</p>
  
  <p>Code sample:</p>
  
  <pre><code>//Server
  static void Main(string[] args)
      {
          using (NamedPipeServerStream server = new NamedPipeServerStream("EOG", PipeDirection.In))
          {
              StreamReader sr = new StreamReader(server);
  
              while (true)
              {
                  server.WaitForConnection();
                  Console.WriteLine("Conected to client");
                  try
                  {
                      Console.WriteLine(sr.ReadLine());
                      server.Disconnect();
                  }
                  catch (Exception e)
                  {
                      Console.WriteLine(e.Message);
                  }
              }
              sr.Dispose();
          }
      }
  }
  
  
  
  //Client
  static void Main(string[] args)
      {
          using (NamedPipeClientStream client = new NamedPipeClientStream(".", "EOG", PipeDirection.Out))
          {
              StreamWriter sw = new StreamWriter(client);
              while (true)
              {
                  Console.WriteLine("Message: ");
                  sw.WriteLine(Console.ReadLine());
                  if (!client.IsConnected)
                      client.Connect();
                  sw.Flush();
              }
              sw.Dispose();
          }
          Console.ReadKey();
      }
  </code></pre>