title: How can you run an HttpListener and TcpListener on different IP addresses with the same port?
body: <p>I want to listen for HTTP requests and TCP connections on the same port but on different IP addresses.</p>
  
  <pre><code>string prefix = "http://192.168.1.2:40000/";
  HttpListener http = new HttpListener();
  http.Prefixes.Add(prefix);
  
  IPEndPoint ep = new IPEndPoint(IPAddress.Parse("192.168.1.3"), 40000);
  TcpListener tcp = new TcpListener(ep);
  </code></pre>
  
  <p>If I start the HttpListener first, I get an error when starting the TcpListener.</p>
  
  <blockquote>
    <p>An attempt was made to access a socket in a way forbidden by its access permissions</p>
  </blockquote>
  
  <p>If I start the TcpListener first, I get an error when starting the HttpListener.</p>
  
  <blockquote>
    <p>The process cannot access the file because it is being used by another process</p>
  </blockquote>
  
  <p>When the HttpListener is running, netstat reveals that it's only listening on the IP address specified, but it's running in the System process (PID 4).</p>
  
  <pre><code>Proto  Local Address          Foreign Address        State           PID
  TCP    192.168.1.2:40000      0.0.0.0:0              LISTENING       4
  </code></pre>
  
  <p>When the TcpListener is running, it's also only listening on the IP address specified, but it's running in my application's process.</p>
  
  <pre><code>Proto  Local Address          Foreign Address        State           PID
  TCP    192.168.1.3:40000      0.0.0.0:0              LISTENING       18316
  </code></pre>
  
  <p>Even though they listen on different IP addresses, there's still a conflict that won't let me do both at the same time.</p>
  
  <p>I am able to run two HttpListeners and two TcpListeners on different IP addresses with the same port, however.</p>
  
  <hr>
  
  <p><strong>Update</strong></p>
  
  <p>The question was asked:</p>
  
  <blockquote>
    <p>How do you have assigned two local IP addresses on the same LAN segment?</p>
  </blockquote>
  
  <p>Originally I had two IP addresses on the same network adapter in the same subnet (255.255.0.0). (See how this is possible at <a href="https://superuser.com/questions/571575/connect-to-two-lan-networks-with-a-single-card">https://superuser.com/questions/571575/connect-to-two-lan-networks-with-a-single-card</a>).</p>
  
  <p>To rule out this as an issue, I setup a virtual machine with two network adapters on different subnets. The results were the same.</p>