Figure out the actual address of the command port a little differently, in a way that actually works.

--
MOS_MIGRATED_REVID=120997894
diff --git a/src/main/cpp/blaze.cc b/src/main/cpp/blaze.cc
index 6a160a7..a5c35e3 100644
--- a/src/main/cpp/blaze.cc
+++ b/src/main/cpp/blaze.cc
@@ -1870,7 +1870,8 @@
   std::string server_dir = globals->options.output_base + "/server";
   std::string port;
   std::string ipv4_prefix = "127.0.0.1:";
-  std::string ipv6_prefix = "::1:";
+  std::string ipv6_prefix_1 = "[0:0:0:0:0:0:0:1]:";
+  std::string ipv6_prefix_2 = "[::1]:";
 
   if (!ReadFile(server_dir + "/command_port", &port)) {
     return false;
@@ -1878,7 +1879,8 @@
 
   // Make sure that we are being directed to localhost
   if (port.compare(0, ipv4_prefix.size(), ipv4_prefix)
-      && port.compare(0, ipv6_prefix.size(), ipv6_prefix)) {
+      && port.compare(0, ipv6_prefix_1.size(), ipv6_prefix_1)
+      && port.compare(0, ipv6_prefix_2.size(), ipv6_prefix_2)) {
     return false;
   }
 
diff --git a/src/main/java/com/google/devtools/build/lib/server/GrpcServerImpl.java b/src/main/java/com/google/devtools/build/lib/server/GrpcServerImpl.java
index a621314..e7311f9 100644
--- a/src/main/java/com/google/devtools/build/lib/server/GrpcServerImpl.java
+++ b/src/main/java/com/google/devtools/build/lib/server/GrpcServerImpl.java
@@ -41,7 +41,6 @@
 import java.io.IOException;
 import java.io.OutputStream;
 import java.lang.reflect.Field;
-import java.net.InetAddress;
 import java.net.InetSocketAddress;
 import java.nio.channels.ServerSocketChannel;
 import java.nio.charset.Charset;
@@ -266,8 +265,7 @@
   @Override
   public void serve() throws IOException {
     Preconditions.checkState(!serving);
-    InetAddress loopbackAddress = InetAddress.getLoopbackAddress();
-    server = NettyServerBuilder.forAddress(new InetSocketAddress(loopbackAddress, port))
+    server = NettyServerBuilder.forAddress(new InetSocketAddress("localhost", port))
         .addService(CommandServerGrpc.bindService(this))
         .build();
 
@@ -285,11 +283,7 @@
     }
     serving = true;
 
-    if (port == 0) {
-      port = getActualServerPort();
-    }
-
-    writeServerFile(PORT_FILE, loopbackAddress.getHostAddress() + ":" + Integer.toString(port));
+    writeServerFile(PORT_FILE, getAddressString());
     writeServerFile(REQUEST_COOKIE_FILE, requestCookie);
     writeServerFile(RESPONSE_COOKIE_FILE, responseCookie);
 
@@ -313,12 +307,16 @@
    * <p>The implementation is awful, but gRPC doesn't provide an official way to do this:
    * https://github.com/grpc/grpc-java/issues/72
    */
-  private int getActualServerPort() {
+  private String getAddressString() {
     try {
       ServerSocketChannel channel =
           (ServerSocketChannel) getField(server, "transportServer", "channel", "ch");
       InetSocketAddress address = (InetSocketAddress) channel.getLocalAddress();
-      return address.getPort();
+      String host = address.getAddress().getHostAddress();
+      if (host.contains(":")) {
+        host = "[" + host + "]";
+      }
+      return host + ":" + address.getPort();
     } catch (IllegalAccessException | NullPointerException | IOException e) {
       throw new IllegalStateException("Cannot read server socket address from gRPC");
     }