Avoid binding a DatagramSocket before setReuseAddress

The API docs suggest the behavior of setResueAddress is undefined
if performed after a socket bind. In reality Android can cope with
it but it is easier to stop relying on that than make excuses.

Bug: 15413832
Change-Id: Ic4c19d58b410e7af9019a1727dc2b7b94e9fb81b
diff --git a/harmony-tests/src/test/java/org/apache/harmony/tests/java/net/DatagramSocketTest.java b/harmony-tests/src/test/java/org/apache/harmony/tests/java/net/DatagramSocketTest.java
index d9f3d91..e8f8e29 100644
--- a/harmony-tests/src/test/java/org/apache/harmony/tests/java/net/DatagramSocketTest.java
+++ b/harmony-tests/src/test/java/org/apache/harmony/tests/java/net/DatagramSocketTest.java
@@ -819,7 +819,7 @@
     }
 
     public void test_getReuseAddress() throws Exception {
-        DatagramSocket theSocket = new DatagramSocket();
+        DatagramSocket theSocket = new DatagramSocket(null);
         theSocket.setReuseAddress(true);
         assertTrue("getReuseAddress false when it should be true", theSocket.getReuseAddress());
         theSocket.setReuseAddress(false);
diff --git a/luni/src/main/java/java/net/MulticastSocket.java b/luni/src/main/java/java/net/MulticastSocket.java
index 24e66c5..2d8fdd6 100644
--- a/luni/src/main/java/java/net/MulticastSocket.java
+++ b/luni/src/main/java/java/net/MulticastSocket.java
@@ -41,7 +41,9 @@
      * @throws IOException if an error occurs.
      */
     public MulticastSocket() throws IOException {
+        super((SocketAddress) null);
         setReuseAddress(true);
+        bind(null);
     }
 
     /**
@@ -51,8 +53,7 @@
      * @throws IOException if an error occurs.
      */
     public MulticastSocket(int port) throws IOException {
-        super(port);
-        setReuseAddress(true);
+        this(new InetSocketAddress(Inet6Address.ANY, port));
     }
 
     /**
@@ -64,8 +65,11 @@
      * @throws IOException if an error occurs.
      */
     public MulticastSocket(SocketAddress localAddress) throws IOException {
-        super(localAddress);
+        super((SocketAddress) null);
         setReuseAddress(true);
+        if (localAddress != null) {
+            bind(localAddress);
+        }
     }
 
     /**