title: Make HTTP requests without opening a new connection
body: <p>If a browser opens a connection to a remote server, is it possible to access <em>that same connection</em> via Javascript?</p>
  
  <p>I have a small Ethernet module on my network that I program sort of like this (pseudocode):</p>
  
  <pre><code>private var socket
  while(true) {
      if(socket is disconnected) {
          open socket
          listen on socket (port 80)
      }
      if(connection interrupt) {
          connect socket
      }
      if(data receive interrupt) {
          serve
      }
      if(disconnection interrupt) {
          disconnect socket
      }
  }
  </code></pre>
  
  <p>The point is that it listens on <em>one socket</em> for HTTP requests and serves them.</p>
  
  <p>In my web browser, I can connect to the device, making an HTTP GET request for some HTML/JS that I've written, and it works. A connection is opened on the socket and the files come back as HTTP responses.</p>
  
  <p>Now I want to click a button on the webpage and have the browser send an HTTP POST request over that same connection. In my Javascript, I have (edited and formatted for clarity):</p>
  
  <pre><code>// This function sends an HTTP request
  function http(type, url, data, callbacks) {
      // make a new HTTP request
      var request = new XMLHttpRequest();
  
      // open a connection to the URL
      request.open(type, url + (data ? "?" + data : ""));
  
      // add headers
      if(type == "POST")
          request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  
      // register callbacks for the request
      Object.keys(callbacks).forEach(function(callback) {
          request[callback] = function() {
              callbacks[callback](request.responseText);
          };
      });
  
      // send and return the request
      request.send();
      return request;
  }
  
  // Here is where I call the function
  http("POST",                // use POST method
       "http://192.168.1.99", // IP address of the network device
       dataToSend,            // the data that needs to be sent
       {                      // callbacks
           onloadend: function(data) {
               console.log("success. got: " + data); // print 'success' when the request is done
           },
           onerror: function(data) {
               console.log("There was an error.");   // print 'error' when it fails
               console.log(data);
           }
       }
  );
  </code></pre>
  
  <p>The issue here is that this <em>opens a new connection</em> to the device, but I want to use the same socket that the browser is already connected to. Is this possible, and if so, how?</p>