title: SocketException: No Connection Could be Made Because The Target Machine Actively Refused it
body: <p>So, I've seen several questions on Stackoverflow that have to do with this, but (a) none of them solved my problem, and (b) they weren't doing what I was doing in most of them. So, I have a client and server windows forms application, and I just want to get them connected. Here's the code for the server:</p>
  
  <pre><code>private void connectButton_Click(object sender, EventArgs e) {
      // Attempt to connect
      try {
          IPAddress ip = IPAddress.Parse(ipBox.Text);
          TcpListener listener = new TcpListener(ip, 8000);
          listener.Start();
          messageBox.Text = "Server started! Waiting for connection...";
          messageBox.Refresh();
          Socket socket = listener.AcceptSocket();
          messageBox.Text += "\nConnection accepted from " + socket.RemoteEndPoint;
      }
      catch (Exception eg) {
          messageBox.Text = Convert.ToString(eg);
      }
  }
  </code></pre>
  
  <p>This all works great, I would include more about getting the IP and all that, but it's pretty straightforward and I already know that that works. So when you run this, and press the connect button, it basically sends the message that it's started. Here's the code for the client:</p>
  
  <pre><code>private void connectButton_Click(object sender, EventArgs e) {
      try {
          TcpClient tcp = new TcpClient();
          messageBox.Text = "Connecting....";
          messageBox.Refresh();
          tcp.Connect(ipBox.Text, 8001);
          messageBox.Text += "\nConnected!";
          messageBox.Refresh();
      }
      catch (Exception eg) {
          messageBox.Text += Convert.ToString(eg);
          messageBox.Refresh();
      }
  }
  </code></pre>
  
  <p>This sends that it is connecting, and then a few seconds later the <a href="https://i.stack.imgur.com/VV6EK.png" rel="nofollow noreferrer">error</a>. I hear this is because the server isn't listening (I've already let the program through the firewall), but I have this on the server: <code>Socket socket = listener.AcceptSocket();</code>. How can I fix this?</p>