title: socket closing if user user exists
body: <p>I'm trying to create a server/client application for a school project.</p>
  
  <p>I have a server on a given port, multiple users connected, and each user has their own username.</p>
  
  <p>Now I want to check if a user that tries to connect is using a valid username.  For example if a user with username A is already connected a new user that want to connect cannot use the username A.</p>
  
  <p>If this happens the server responds to the new client with an error code.</p>
  
  <p>This is the code for this part:</p>
  
  <pre><code>   private void Receive()
      {
          while (true)
          {
              byte[] buffer = new byte[64];
              socket.Receive(buffer);
              string received = Encoding.Default.GetString(buffer);
              if (received.IndexOf("!error") != -1)
              {
                  string[] mySplit = received.Split(':');
                  string errorCode = mySplit[1].Trim((char)0);
                  if (errorCode == "user exists")
                  {
                      richTextBox1.AppendText("Your connection was refused by server, because there's already another user connected with the username you choose");
                      socket.Disconnect(true);
  
                      connectBtn.Enabled = true;
                  }
              }
          }
      }
  </code></pre>
  
  <p>When I try to do this the program crashs and visual studio says that there's an invalid
  cross-thread operation on <code>richTextBox1</code>.</p>