public static String getHostAddress()
      throws SocketException, UnknownHostException {
    boolean isIPv6Preferred = Boolean.parseBoolean(System.getProperty("java.net.preferIPv6Addresses"));
    DatagramSocket ds = new DatagramSocket();
    try {
      ds.connect(isIPv6Preferred ? Inet6Address.getByName(DUMMY_OUT_IPV6) : Inet4Address.getByName(DUMMY_OUT_IPV4),
          HTTP_PORT);
    } catch (java.io.UncheckedIOException e) {
      LOGGER.warn(e.getMessage());
      if (isIPv6Preferred) {
        LOGGER.warn("No IPv6 route available on host, falling back to IPv4");
        ds.connect(Inet4Address.getByName(DUMMY_OUT_IPV4), HTTP_PORT);
      } else {
        LOGGER.warn("No IPv4 route available on host, falling back to IPv6");
        ds.connect(Inet6Address.getByName(DUMMY_OUT_IPV6), HTTP_PORT);
      }
    }

    InetAddress localAddress = ds.getLocalAddress();
    if (localAddress.isAnyLocalAddress()) {
      localAddress = isIPv6Preferred ? getLocalIPv6Address() : InetAddress.getLocalHost();
    }

    return localAddress.getHostAddress();
  }
@Test(description = "Test getHostAddress with no preferIPv6Addresses in dual stack environment")
  public void testGetHostAddressDualStackEnv() {
    InetAddress mockInetAddress = mock(InetAddress.class);
    when(mockInetAddress.isAnyLocalAddress()).thenReturn(false);
    when(mockInetAddress.getHostAddress()).thenReturn(LOCAL_ADDRESS_IPV4);

    try (MockedConstruction<DatagramSocket> mockedConstructionDatagramSocket = mockConstruction(DatagramSocket.class,
        initDatagramSocket(mockInetAddress, NetworkEnv.DUAL_STACK))) {
      String hostAddress = NetUtils.getHostAddress();
      DatagramSocket mockDatagramSocket = mockedConstructionDatagramSocket.constructed().get(0);

      assertEquals(LOCAL_ADDRESS_IPV4, hostAddress);
      assertEquals(1, mockedConstructionDatagramSocket.constructed().size());
      verify(mockDatagramSocket, times(1)).connect(any(), anyInt());
    } catch (SocketException | UnknownHostException e) {
      Assert.fail("Should not throw: " + e.getMessage());
    }
  }