title: Complex socket application with multiple clients and web interface
body: <p>I have created an asp.net website. On that website the user can create webpages and put buttons. These buttons sends commands via web socket to a tray application I implemented. The tray application then takes the commands and passes them along to various third-party applications using TCP connections. the third-party applications then perform the commands and send back their status to the tray application. The tray application writes the status into an sql database. The website reads the status from the database and processes the status (for example, it highlights the button that send the original command)</p>
  
  <p>Right now it is a mess. For example the third-party applications all handle the communication in different ways. How would I best go about organizing such an application?</p>
  
  <p>I use ab sql database in between because I cannot be sure that the user wont reload the website. And then what happens to the web socket? Also, it can update its buttons on its own pace. Is that a good solution?</p>
  
  <p>Also, my knowledge of TCP and socket programming is sorely lacking. Most resources, tutorials, guides just give really simple examples. Isn't there something better out there? (books, articles)</p>
  
  <p>I am using a TCPListener that accepts the website requests in an infinite loop:</p>
  
  <pre><code>static void TcpListenerWorker_DoWork(object sender, DoWorkEventArgs e)
  {
      server = new TcpListener(IPAddress.Parse(Properties.Settings.Default.TrayAppServer), Properties.Settings.Default.TrayAppPort);
  
      // Start listening for client requests.
      server.Start();
  
      // Buffer for reading data
      Byte[] bytes = new Byte[256];
      String data = null;
  
      // Enter the listening loop.
      while (true)
      {
          if (TcpListenerWorker.CancellationPending)
              break;
  
          // Perform a blocking call to accept requests.
          // You could also user server.AcceptSocket() here.
          TcpClient client = server.AcceptTcpClient();
  
          ...
  </code></pre>
  
  <p>Is there a better way to link the tray application to the website?</p>
  
  <p>The tray application also has to check if the third-party application is running. Only if it is running, should the socket be created. If the third-party application crashes to connection is gone. How do I reestablish the connection properly?</p>
  
  <p>The user should now if he can send commands. For that I check if the application is running like so </p>
  
  <pre><code>Process.GetProcessesByName("exApp1");
  </code></pre>
  
  <p>Is there a better way to check for the applications? Maybe by trying to establish a tcp connection and if it fails, the app is not running. But relying on a failed connection attempt might not be good decision.</p>
  
  <p>A lot of errors occur. Connections disappear, commands do not arrive. Are there APIs I could use to handle these things better (better error and exception handling)?</p>
  
  <p>Sorry, if all that sounds confusing. The entire project is a mess. Please tell me if I should explain more.</p>