title: c# SSL TCPServer stuck at SsLStream.AuthenticateAsServer()
body: <p><strong>Storyline:</strong></p>
  
  <p>I wanted to make my very own webserver in c#(first attempt). It went well(I was using <strong>Visual Studio to code</strong> the application and <strong>Firefox to check</strong> if I was doing right) and I managed to make a basic TCPServer. As I was trying to add SSL support to it I encountered a problem.</p>
  
  <ul>
  <li>I have been trying to authenticate as a TcpServer with SSL support using SSLStream.AuthenticateAsServer([self-signed certificate]) for the last 7 days</li>
  </ul>
  
  <p><strong>Problem:</strong></p>
  
  <p>As I get the [Upgrade-Insecure-Requests: 1] from my beloved Firefox I'm trying to [SSLStream.AuthenticateAsServer([self-signed certificate])]. Doing that <strong>my code is stuck(but doesn't freeze/crash)</strong> at this line while <strong>my Firefox is just loading forever</strong> without seeming to send me a Response.</p>
  
  <p><strong>Code:</strong>
  <hr>
  starting my server</p>
  
  <hr>
  
  <pre><code>TCPServer.ServerStart(8080);
  </code></pre>
  
  <p><hr>
  (listener is being defined in my TCPServer class)</p>
  
  <pre><code>internal static TcpListener listener;
  </code></pre>
  
  <hr>
  
  <pre><code>async internal static void ServerStart(int port)
      {
          if (listener == null)
          {
              listener = new TcpListener(IPAddress.Any, port);
          }
          listener.Start();
  
          //clients
          await Task.Run(()=&gt; SucheNachBesuchern());
          listener.Stop();
      }
  </code></pre>
  
  <p><hr>
  accepting clients</p>
  
  <hr>
  
  <pre><code>private static void SucheNachBesuchern(){
          TcpClient Besucher;
  
          while (true)
          {
              Besucher = listener.AcceptTcpClient();
              ThreadPool.QueueUserWorkItem(ThreadProzess, Besucher);
          }
      }
  </code></pre>
  
  <p><hr>
  handling accepted clients</p>
  
  <hr>
  
  <pre><code>private static void ThreadProzess(object Besucher) {
          TcpClient besucher = (TcpClient)Besucher;
          Abfertige(besucher);
          besucher.Close();
          besucher.Dispose();
      }
  </code></pre>
  
  <hr>
  
  <pre><code>private static void Abfertige(TcpClient Besucher)
      {
          //Reading the Request
          StreamReader Auftrag = new StreamReader(Besucher.GetStream());
          List&lt;String&gt; AuftragNachricht= new List&lt;String&gt;();
          while (Auftrag.Peek()!=-1) {
              AuftragNachricht.Add(Auftrag.ReadLine());
          }
  
          //Anfrage = request Class with bool Anfrage.SSLAnfrage being true
          //if the request contains 'Upgrade-Insecure-Requests: 1'
          Anfrage Anfrage = Anfrage.VerarbeiteAuftrag(AuftragNachricht);
  
          if (Anfrage.SSLAnfrage)// = if([request conatined 'Upgrade-Insecure-Requests: 1'])
          {
              //opening an SslStream to the TcpClient Besucher
              SslStream SSLStream = new SslStream(Besucher.GetStream(), false);
              try
              {
                  //Authenticating as TcpServer supporting SSL !CODE IS STUCK AT THIS LINE!
                  SSLStream.AuthenticateAsServer([SELFSINGEDX509CERTIFICATE.cer using openssl pkcs12], clientCertificateRequired: false, enabledSslProtocols: System.Security.Authentication.SslProtocols.Default, checkCertificateRevocation: false);
  
                  //set timeouts for read and write
                  SSLStream.ReadTimeout = 5000;
                  SSLStream.WriteTimeout = 5000;
  
                  //tryig to catch my Firefox as new client on SSL port 443
                  TcpListener SSLListener = new TcpListener(IPAddress.Parse("192.168.178.72"), 443); 
                  SSLListener.Start();
                  TcpClient SSLBesucher = SSLListener.AcceptTcpClient();
                  Debug.WriteLine("I'VE GOT A CLIENT HERE!!!!111");
              }
              catch (Exception Error) {
                  Debug.WriteLine($"---Error gefangen: {Error.ToString()}");
              }
          }//[...more Code]
  </code></pre>
  
  <p><hr>
  (For not having any knowledge in SSL <a href="https://docs.microsoft.com/en-us/dotnet/api/system.net.security.sslstream?view=netframework-4.7.2" rel="nofollow noreferrer">I used this example-code</a>)</p>