title: Check if a port is assigned to any program or added in firewall - C#
body: <p>We have Windows Service which will be installed by installer. We have an option to allow user to provide a port number and select whether the service must start on completion of installation. We are having a check on installer itself for checking whether the port is open/available.</p>
  
  <pre><code>TcpClient TcpScan = new TcpClient();
  TcpScan.Connect("localhost", portno);
  if (TcpScan.Connected == true)
  {
     TcpScan.Close();
  }
  </code></pre>
  
  <p>My problem is if the user selects the option of not to start the service on installation and then we install another instance on the same machine with the same port as used in first one, then if we start both the services then one of the service will not work.</p>
  
  <p>So is there any way I can check whether the port provided by user is already there in firewall or is already assigned to some other windows service? (Also assume the service can be in stopped state) </p>