title: How to use Multithreading, locks, Socket Programming
body: <p>For last 48 hours, I have been trying to understand <code>Multithreading</code> and <code>Socket Programming</code>. I tried to  implement socket programming and had success when not using multithreading. I am new to both of the topics and have raised 2-3 question on stack itself needing help on the same. </p>
  
  <p>After googling a lot I found an <strong><a href="http://msdn.microsoft.com/en-us/magazine/cc300760.aspx" rel="nofollow noreferrer">article</a></strong> that explains <code>Socket Programming</code> and <code>Multithreading</code>, but I still have a lot of doubts in this article and got stuck at <code>Figure 5</code> in the article. </p>
  
  <pre><code>private void AcceptConnections()
      {
          while (true)
          {
              // Accept a connection
              Socket socket = _serverSocket.Accept();
              ConnectionInfo connection = new ConnectionInfo();
              connection.Socket = socket;
  
              // Create the thread for the receives.
              connection.Thread = new Thread(ProcessConnection);
              connection.Thread.IsBackground = true;
              connection.Thread.Start(connection);
  
              // Store the socket
              lock (_connections) _connections.Add(connection);
          }
      }
  </code></pre>
  
  <p>In the very last line you can see  a <code>lock</code> has been taken and 3-4 lines above a <code>delegate ProcessConnection</code> is bound.</p>
  
  <p>At this point, I am not clear how this lock is working. What is happening behind the scenes when the lock has taken? Why did the author use lock here? What would have happened if no lock was taken? How does the thread ProcessConnection work? What things are happening simultaneously?</p>
  
  <p>I got confused with all these questions</p>
  
  <p>I know there is a list of questions here, but it would be a great help if you could assist me in understanding the methodology of working with multithreading.</p>