title: Socket can't be created, throws IOException
body: <p>I've created a chat program which required the user to select either the client or server role. My approach at removing this requirement is to have every user start their own server where they'll get messages from. This should allow me to have two clients talk to the the other without having to put a server in between them.</p>
  
  <p>Right now I've modified my program in such a way that the client side does the sending and the server does the receiving.</p>
  
  <p>Note that communication between the two programs worked perfectly fine up untill these changes. However, now that I've changed some stuff an error occurs as early as when I create a socket.</p>
  
  <p>The flow of my program untill the problem is as follows:</p>
  
  <ul>
  <li>Program starts </li>
  <li>Server starts automatically, binded to local port 6666</li>
  <li>Connection config pops up, user clicks the save button (target host and port are saved)</li>
  <li>User clicks the connect button</li>
  <li>Program creates a client thread</li>
  <li>Thread creates the socket and initiates the outbound stream</li>
  </ul>
  
  <p>After some debugging I've found that this socket is never created.</p>
  
  <p>When the flow enters this stage (last item in the list), only the 'First test' gets executed. </p>
  
  <pre><code>public void run() {
              System.out.println("First test");
              createConnection();
              System.out.println("Second test");
              initiateIO();
      }
  
  private void createConnection() {
          try {
              socket = new Socket(host, port);
          } catch (UnknownHostException e) {
              OutputUtil.showErrorMessage("Couldn't bind socket to unknown host", "Unknown host");
          } catch (IOException e) {
              OutputUtil.showErrorMessage("General IO error at creating client socket", "IO error");
          }
      }
  
  private void initiateIO() {
          try {
              outbound = new PrintWriter(socket.getOutputStream(), true);
          } catch (IOException e) {
              OutputUtil.showErrorMessage("Couldn't load IO streams from client", "IO Error");
          }
      }
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>Console: First test
  Popup: General IO error at creating client socket
  Console: Second test
  Console: NPE at `outbound.close()`
  </code></pre>
  
  <p>I'm assuming the NPE is a result of the first error, considering a method from the socket is invoked when creating the <code>PrintWriter</code>. It should also be noted that it takes around 10 seconds to show the first error.</p>
  
  <p>At first I thought the error might be introduced because both the local server and the connection with the other client use port 6666, but after creating a link on port 6667 the problem still occurred. Which makes sense upon review.</p>
  
  <p>When my debugger points at the line where <code>outbound</code> is initialized (after the "second test" message, socket has value <code>null</code>.</p>
  
  <p>My question is: why can't the socket be created? The documentation only specifies </p>
  
  <blockquote>
    <p>IOException - if an I/O error occurs when creating the socket.</p>
  </blockquote>
  
  <p>which isn't of much use.</p>
  
  <p>Full source code can be found <a href="https://github.com/Vannevelj/Messy" rel="nofollow">here</a> for a better overview.</p>
  
  <p>Edit: Printed the stacktrace from the first, main error.</p>
  
  <pre><code>java.net.ConnectException: Connection timed out: connect
      at java.net.DualStackPlainSocketImpl.connect0(Native Method)
      at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
      at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
      at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
      at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
      at java.net.PlainSocketImpl.connect(Unknown Source)
      at java.net.SocksSocketImpl.connect(Unknown Source)
      at java.net.Socket.connect(Unknown Source)
      at java.net.Socket.connect(Unknown Source)
      at java.net.Socket.&lt;init&gt;(Unknown Source)
      at java.net.Socket.&lt;init&gt;(Unknown Source)
      at core.Client.createConnection(Client.java:30)
      at core.Client.run(Client.java:64)
      at java.lang.Thread.run(Unknown Source)
  </code></pre>