title: An operation was attempted on a nonexistent network connection, error code 1229
body: <p>Just working on a nice and simple HttpListener and all of the sudden this exception pops-up.</p>
  <blockquote>
  <p>An operation was attempted on a nonexistent network connection.</p>
  </blockquote>
  <p>Look hours for a solution and could not find one. The code crashed as soon as i submit data from the webpage to the <code>HttpListener</code>.
  Can someone please explain to me how i can fix this issue?</p>
  <p>The Code is below:</p>
  <p><strong>C#</strong></p>
  <pre><code>public static void StartListening()
      {
          
          Stream ouputStream;
          
          
          HttpListener listener = new HttpListener();
          SetPrefixes(listener);
          
          if (listener.Prefixes.Count &gt; 0)
          {
              listener.Start();
             
              Console.WriteLine(&quot;HttpClient Started&quot;);  
  
              while(true)
              {
                  HttpListenerContext context = listener.GetContext();
                  HttpListenerRequest request= context.Request;
                  HttpListenerResponse response = context.Response;
                 
                  string html = Properties.Resources.index;
                  byte[] webPageBuffer = Encoding.UTF8.GetBytes(html);
                                      
                  response.ContentLength64 = webPageBuffer.Length;
                  ouputStream = response.OutputStream;
                  ouputStream.Write(webPageBuffer, 0, webPageBuffer.Length);
  
                  ouputStream.Flush();
  
                  Common.Wait(2000);  
                                   
                  String url = request.RawUrl;
                  String[] queryStringArray = url.Split('/');
                  String postedtext = GetPostedText(request);                   
  
                  byte[] buffer = null;
                  
                  // Lots of if statements because a switch would not work here.
                  if(queryStringArray[0] == &quot;myForm&quot;)
                  {
                      buffer = System.Text.Encoding.UTF8.GetBytes(&quot;I recieved myForm&quot;);
                  }
                  if(queryStringArray[1] == &quot;doSomething&quot;)
                  {
                      buffer = System.Text.Encoding.UTF8.GetBytes(&quot;I recieved doSomething&quot;);
                  }
                  if(buffer != null)
                  {
                      response.AddHeader(&quot;Cache-Control&quot;, &quot;no-cache&quot;);
                      response.AddHeader(&quot;Acces-Control-Allow-Origin&quot;,&quot;*&quot;);
  
                      response.ContentLength64 = buffer.Length;
                      ouputStream = response.OutputStream;
                      ouputStream.Write(buffer, 0, buffer.Length);
                      ouputStream.Close();
                  }
              }
          }
      }
                 
      private static void SetContext(HttpListenerContext context, Stream ouputStream)
      {
          // De GetContext methode blokkeert terwijl die wacht op een aanvraag(request)
          
          
      }
  
      private static void SetPrefixes(HttpListener listener)
      {
          String[] prefixes = new String[] { &quot;http://localhost:8100/&quot;, &quot;http://192.168.33.28:8000/&quot; };
  
          int i = 0;
  
          foreach (string s in prefixes)
          {
              listener.Prefixes.Add(s);
              i++;
          }
      }
  
  
      private static string GetPostedText(HttpListenerRequest request)
      {
          string recievedText;
  
          using(StreamReader reader = new StreamReader(request.InputStream, request.ContentEncoding))
          {
              recievedText= reader.ReadToEnd();
          }
  
          if (recievedText != &quot;&quot;)
          {
              Console.WriteLine(&quot;{0} RECIEVED: &quot; + recievedText, DateTime.Now);
          }
  
          return recievedText;
      }
  </code></pre>
  <p><strong>HTML</strong></p>
  <pre><code>&lt;html&gt;
      &lt;head&gt; 
          &lt;title&gt;http&lt;/title&gt;
          &lt;meta charset=&quot;utf-8&quot;&gt;
          &lt;script src=&quot;https://code.jquery.com/jquery-1.10.2.js&quot;&gt;&lt;/script&gt;
          &lt;style&gt;
  
              body{
          font-size: 12px;
          font-family: Arial;
          }
  
          legend{
              font-weight: bold;
              font-size: 14px !important;
          }
  
  
          fieldset{
              width:200px;
              margin: 0;
              position: absolute;
              top: 50%;
              left: 50%;
              margin-right: -50%;
              transform: translate(-50%, -50%) }
  
          .wrapper{
              width: 100%;
          }
  
          &lt;/style&gt;
  
  
          &lt;script language=&quot;javascript&quot;&gt;
  
          //&lt;!-- Create variable timer --&gt;
              var timer;
  
  
          //&lt;!-- Create Fucntion CheckOne --&gt;
          /* 
              Wannneer de functie aangroepen word, word eerst de timeout van de variable timer geleegd.
              Daarna word er een timeout ingesteld van 2000 (2sec). Die timeout wordt ingeschakeld nadat het textveld niet meer actief beschouwd word(niet meer gebruikt word, de focus blijft wel op het veld.).
              Na die 2 seconden krijgt de volgende textbox de focus met de zelfde manier maar onder een andere functie.
              Zodra hier ook de 2 seconden om zijn verspringt de focus weer maar nu naar een sumbit (verzenden) knop. Dit is gedaan omdat je dan makkelijk op een OK knop kan drukken op het apparaat.
          */
  
              function CheckOne() {
                   clearTimeout(timer)
                   timer = setTimeout(function(){
                       document.getElementById(&quot;two&quot;).focus();
                       //clearTimeout(timer);
                   }, 750)
              }
  
              function CheckTwo(){
                  clearTimeout(timer);
                  timer = setTimeout(function(){
                       document.getElementById(&quot;sub&quot;).focus();
                   }, 750)
              }
  
  
          &lt;/script&gt; 
          
  
  
  
      &lt;/head&gt; 
  
      &lt;body&gt; 
  
      &lt;div class=&quot;wrapper&quot;&gt; 
          &lt;fieldset&gt; 
              &lt;legend&gt;HttpListener &lt;/legend&gt;&lt;br/&gt; 
              &lt;form id=&quot;searchForm&quot; action=&quot;http://localhost:8100/myForm/doSomething&quot; &gt;
                  Locatienummer: &lt;br /&gt;
                  &lt;input type=&quot;text&quot; id=&quot;one&quot; onkeyup=&quot;CheckOne()&quot; name=&quot;locatienummer&quot;&gt;&lt;br /&gt;
                  Bonnummer: &lt;br /&gt;
                  &lt;input type=&quot;text&quot; id=&quot;two&quot; onkeyup=&quot;CheckTwo()&quot; name=&quot;bonnummer&quot;&gt;&lt;br /&gt;&lt;br /&gt;
                  &lt;input id=&quot;sub&quot; type=&quot;submit&quot; value=&quot;Verzenden&quot; /&gt;
              &lt;/form&gt; 
          &lt;/fieldset&gt; 
      &lt;/div&gt; 
      &lt;!-- Include the needed files--&gt; 
  
          &lt;script&gt;
      $(&quot;#searchForm&quot;).submit(function (event)
      {
          // Stop form from submitting normally
          event.preventDefault();
  
          // Get some values from elements on the page:
          var $form = $(this),
  
          locatieValue = $form.find(&quot;input[name='locatienummer']&quot;).val(),
          bonValue = $form.find(&quot;input[name='bonnummer']&quot;).val(),
  
          url = $form.attr(&quot;action&quot;);
  
          // Send the data using post
          $.post(url, { a: locatieValue, b: bonValue });
  
      });
          &lt;/script&gt;
  
      &lt;/body&gt; 
  &lt;/html&gt;
  </code></pre>