title: How can I receive data from socket continuously?
body: <p>I'm trying to receive data from a socket via Windows Service, but when I make a connection it becomes instant. I want to receive it constantly (It will disconnect when port is closed). How can I achieve this?</p>
  
  <p>This is my output for now;</p>
  
  <p>"GET / HTTP/1.1</p>
  
  <p>Host: 127.0.0.1:1994</p>
  
  <p>Connection: keep-alive</p>
  
  <p>Cache-Control: max-age=0</p>
  
  <p>Upgrade-Insecure-Requests: 1</p>
  
  <p>User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36</p>
  
  <p>Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,<em>/</em>;q=0.8</p>
  
  <p>Accept-Encoding: gzip, deflate, sdch</p>
  
  <p>Accept-Language: tr-TR,tr;q=0.8,en-US;q=0.6,en;q=0.4</p>
  
  <p>Connection proceed? False"</p>
  
  <pre><code>using System;
  using System.IO;
  using System.Net;
  using System.Text;
  using System.Linq;
  using System.Threading;
  using System.Net.Sockets;
  using System.Diagnostics;
  using System.ComponentModel;
  using System.ServiceProcess;
  using System.Collections.Generic;
  
  namespace AServiceTest
  {
      public partial class Service1 : System.ServiceProcess.ServiceBase
      {
          Thread th;
          bool isRunning = false;
          byte[] bytes = new byte[1024];
  
          public Service1() 
          {
              InitializeComponent();
          }
          protected override void OnStart(string[] args)
          {
              th = new Thread(DoThis);
              th.Start();
              isRunning = true;            
          }
          private void DoThis()
          {
              while (isRunning)
              {
                      Socket listener, connecter, acc;
                      listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                      connecter = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); //Variables
  
                      listener.Bind(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 1994));
                      listener.Listen(0);
  
                      acc = listener.Accept(); //Accepting comm with client part
                      bool IsConnected = !((listener.Poll(1000, SelectMode.SelectRead) &amp;&amp; (listener.Available == 0)) || !listener.Connected);
  
                      File.AppendAllText(@"D:\Tasks\dataGathering.txt", Environment.NewLine + "Connection proceed?1 " + IsConnected);
  
  
                  new Thread(() =&gt;
                  {
  
                      byte[] bytes = new byte[1024];
                      int byteCount = acc.Receive(bytes, SocketFlags.None);
  
                      File.AppendAllText(@"D:\Tasks\dataGathering.txt", Environment.NewLine + "Connection proceed?2 " + IsConnected);
  
                      Console.WriteLine(Encoding.UTF8.GetString(bytes)); //It encodes before writing to the screen
                      File.AppendAllText(@"D:\Tasks\dataGathering.txt", Environment.NewLine + " " + Encoding.UTF8.GetString(bytes));
  
                      File.AppendAllText(@"D:\Tasks\dataGathering.txt", Environment.NewLine + "Connection proceed?3 " + IsConnected);
  
  
                      File.AppendAllText(@"D:\Tasks\dataGathering.txt", Environment.NewLine + "Connection proceed? " + IsConnected);
                      Console.WriteLine("Does connection proceeding? " + IsConnected);
  
                  }).Start();
              }
          }        
          protected override void OnStop() 
          {
              isRunning = false;
              th = null;
          }
          private void InitializeComponent() 
          {
              this.ServiceName = "AServiceTest";
              this.CanStop = true;
              this.AutoLog = false;
              this.EventLog.Log = "Application";
              this.EventLog.Source = "Service1";
          }
      }
  }
  </code></pre>