title: java.net.SocketException: Invalid argument: connect
body: <p>My new laptop (Alienware m17x) throws a <code>java.net.SocketException: Invalid argument: connect</code> when I run the following basic code:</p>
  <p>Server.java:</p>
  <pre><code>public static void main (String[] args) throws Exception {
          
      ServerSocket serverSocket = new ServerSocket (8888);
      Socket socket = serverSocket.accept();
  }
  </code></pre>
  <p>Client.java:</p>
  <pre><code>public static void main (String[] args) throws Exception {
      Socket socket = new Socket (&quot;localhost&quot;, 8888);
  }
  </code></pre>
  <p>Every time I run Client.java (after starting Server.java) I get this socket exception. Here's the full trace of the exception:</p>
  <pre><code>Exception in thread &quot;main&quot; java.net.SocketException: Invalid argument: 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 Client.main(Client.java:5)
  </code></pre>
  <p>I've tried a few things, but no luck the exception is always thrown. Here's what I have concluded:</p>
  <ul>
  <li>Changing the port numbers changes nothing.</li>
  <li>It's not a problem with the network. I have gotten the exception at school and at home.</li>
  <li>I've reinstalled the JVM and the JDK but no luck.</li>
  <li>This only occurs on one machine. I have run the same code on my desktop and I have not gotten any exceptions. I am using Windows 7 on both.</li>
  <li>It is not blocked by a firewall. I have turned my firewalls off and I still have this issue.</li>
  </ul>
  <p>What should I do or check for to resolve this issue?</p>
  <p><strong>EDIT:</strong>
  route print outputs:</p>
  <pre><code>===========================================================================
  Interface List
   15...e4 d5 3d 08 cb 83 ......Killer Wireless-N 1103 Network Adapter
   13...d0 df 9a b5 73 dc ......Bluetooth Device (Personal Area Network)
   11...d4 be d9 00 10 65 ......Atheros AR8151 PCI-E Gigabit Ethernet Controller (NDIS 6.20)
    1...........................Software Loopback Interface 1
   17...00 00 00 00 00 00 00 e0 Teredo Tunneling Pseudo-Interface
   16...00 00 00 00 00 00 00 e0 Microsoft ISATAP Adapter #2
   18...00 00 00 00 00 00 00 e0 Microsoft ISATAP Adapter
   19...00 00 00 00 00 00 00 e0 Microsoft ISATAP Adapter #3
  ===========================================================================
  
  IPv4 Route Table
  ===========================================================================
  Active Routes:
  Network Destination        Netmask          Gateway       Interface  Metric
            0.0.0.0          0.0.0.0      192.168.0.1    192.168.0.191     25
          127.0.0.0        255.0.0.0         On-link         127.0.0.1    306
          127.0.0.1  255.255.255.255         On-link         127.0.0.1    306
    127.255.255.255  255.255.255.255         On-link         127.0.0.1    306
        192.168.0.0    255.255.255.0         On-link     192.168.0.191    281
      192.168.0.191  255.255.255.255         On-link     192.168.0.191    281
      192.168.0.255  255.255.255.255         On-link     192.168.0.191    281
          224.0.0.0        240.0.0.0         On-link         127.0.0.1    306
          224.0.0.0        240.0.0.0         On-link     192.168.0.191    281
    255.255.255.255  255.255.255.255         On-link         127.0.0.1    306
    255.255.255.255  255.255.255.255         On-link     192.168.0.191    281
  ===========================================================================
  Persistent Routes:
    None
  
  IPv6 Route Table
  ===========================================================================
  Active Routes:
   If Metric Network Destination      Gateway
   17     58 ::/0                     On-link
    1    306 ::1/128                  On-link
   17     58 2001::/32                On-link
   17    306 2001:0:4137:9e76:28f3:2721:524f:e2ef/128
                                      On-link
   15    281 fe80::/64                On-link
   17    306 fe80::/64                On-link
   17    306 fe80::28f3:2721:524f:e2ef/128
                                      On-link
   15    281 fe80::68c1:bc79:fefa:88a2/128
                                      On-link
    1    306 ff00::/8                 On-link
   17    306 ff00::/8                 On-link
   15    281 ff00::/8                 On-link
  ===========================================================================
  Persistent Routes:
    None
  </code></pre>
  <p><strong>EDIT:</strong> As @PhilippeLM and @beny23 made me realize, setting the java system variable java.net.preferIPv4Stack to true solves my problem. However I want a permanent fix. I don't want to specify the system variable every time I run a java application.</p>
  <p>Here's what I've tried with no luck once again:</p>
  <ul>
  <li>Setting my machine preferences to prefer IPv4 by following <a href="http://support.microsoft.com/kb/929852" rel="noreferrer">these steps</a></li>
  <li>Adding the line <code>java.net.preferIPv4Stack=true</code> to the <code>net.properties</code> file in <code>%JAVA_HOME%\jre\lib</code>.</li>
  </ul>
  <p>Is there anything else I can try?</p>