title: Android Socket connection refused
body: <p>I am trying to open a Socket on Android but everytime I try I keep getting connection refused error no matter what way I try.</p>
  
  <p>The error happens on the following line of code where I try to create the connection.</p>
  
  <hr>
  
  <pre><code>Socket socket = new Socket(getLocalIpAddressString(), 8008);
  </code></pre>
  
  <hr>
  
  <p>The following is the method I use to get the local ip address:</p>
  
  <hr>
  
  <pre><code>public static String getLocalIpAddressString() {
   try {
       for (Enumeration&lt;NetworkInterface&gt; en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
           NetworkInterface intf = en.nextElement();
           for (Enumeration&lt;InetAddress&gt; enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
               InetAddress inetAddress = enumIpAddr.nextElement();
               if (!inetAddress.isLoopbackAddress()) {
                   return inetAddress.getHostAddress().toString();
               }
           }
       }
   } catch (SocketException ex) {
       Log.e("IPADDRESS", ex.toString());
   }
   return null;
  </code></pre>
  
  <p>}</p>
  
  <hr>
  
  <p>And the error I get is:</p>
  
  <hr>
  
  <pre><code>WARN/System.err(3574): java.net.ConnectException: /192.168.2.163:8008 - Connection refused
  WARN/System.err(3574):     at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:237)
  WARN/System.err(3574):     at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:199)
  WARN/System.err(3574):     at java.net.Socket.startupSocket(Socket.java:734)
  WARN/System.err(3574):     at java.net.Socket.&lt;init&gt;(Socket.java:157)
  WARN/System.err(3574):     at com.networks.phone.engine.SIPEngine.rtpTest(SIPEngine.java:1444)
  </code></pre>
  
  <hr>
  
  <p>I presume its an error with my Ipaddress and or port? But can anyone point me in the right direction as to what might be the cause?</p>