title: Background running server with socket connection
body: <p>now I have this: </p>
  
  <pre><code>[STAThread]
  static void Main()
          {
              if (flag) //client view
                  Application.Run(new Main_Menu());
              else
              {
                  Application.Run(new ServerForm());
              }
          }
  </code></pre>
  
  <p>ServerForm.cs</p>
  
  <pre><code> public partial class ServerForm : Form
      {
          public ServerForm()
          {
              InitializeComponent();
              BeginListening(logBox);
          }
  
          public void addLog(string msg)
          {
              this.logBox.Items.Add(msg);
          }
  
          private void button1_Click(object sender, EventArgs e)
          {
  
          }
          private async void BeginListening(ListBox lv)
          {
              Server s = new Server(lv);
              s.Start();
          }
      }
  </code></pre>
  
  <p>Server.cs</p>
  
  <pre><code>public class Server
      {
          ManualResetEvent allDone = new ManualResetEvent(false);
          ListBox logs;
          ///
          /// 
          /// Starts a server that listens to connections
          ///
          public Server(ListBox lb)
          {
              logs = lb;
          }
          public void Start()
          {
              Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
              listener.Bind(new IPEndPoint(IPAddress.Loopback, 1440));
              while (true)
              {
                  Console.Out.WriteLine("Waiting for connection...");
                  allDone.Reset();
                  listener.Listen(100);
                  listener.BeginAccept(Accept, listener);
                  allDone.WaitOne(); //halts this thread
              }
          }
          //other methods like Send, Receive etc.
  }
  </code></pre>
  
  <p>I would like to run my <code>ServerForm</code> ( it has ListBox to print msg from <code>Server</code>). I know <code>ListBox</code> argument will not work, but I could not run <code>Server</code> inifinite loop without suspend <code>ServerForm</code> ( I could not even move window). I tried it also with Threads - unfortunately it does not work to.</p>