/** Prepares the socket addresses to attempt for the current proxy or host. */
  private void resetNextInetSocketAddress(Proxy proxy) throws UnknownHostException {
    // Clear the addresses. Necessary if getAllByName() below throws!
    inetSocketAddresses = new ArrayList<>();

    String socketHost;
    int socketPort;
    if (proxy.type() == Proxy.Type.DIRECT || proxy.type() == Proxy.Type.SOCKS) {
      socketHost = address.getUriHost();
      socketPort = getEffectivePort(uri);
    } else {
      SocketAddress proxyAddress = proxy.address();
      if (!(proxyAddress instanceof InetSocketAddress)) {
        throw new IllegalArgumentException(
            "Proxy.address() is not an " + "InetSocketAddress: " + proxyAddress.getClass());
      }
      InetSocketAddress proxySocketAddress = (InetSocketAddress) proxyAddress;
      socketHost = getHostString(proxySocketAddress);
      socketPort = proxySocketAddress.getPort();
    }

    // Try each address for best behavior in mixed IPv4/IPv6 environments.
    for (InetAddress inetAddress : network.resolveInetAddresses(socketHost)) {
      inetSocketAddresses.add(new InetSocketAddress(inetAddress, socketPort));
    }
    nextInetSocketAddressIndex = 0;
  }
 /** Returns the next proxy to try. May be PROXY.NO_PROXY but never null. */
 private Proxy nextProxy() throws IOException {
   if (!hasNextProxy()) {
     throw new SocketException(
         "No route to " + address.getUriHost() + "; exhausted proxy configurations: " + proxies);
   }
   Proxy result = proxies.get(nextProxyIndex++);
   resetNextInetSocketAddress(result);
   return result;
 }
 /** Returns the next connection spec to try. */
 private ConnectionSpec nextConnectionSpec() throws IOException {
   if (!hasNextConnectionSpec()) {
     throw new SocketException(
         "No route to "
             + address.getUriHost()
             + "; exhausted connection specs: "
             + connectionSpecs);
   }
   return connectionSpecs.get(nextSpecIndex++);
 }
 /** Returns the next socket address to try. */
 private InetSocketAddress nextInetSocketAddress() throws IOException {
   if (!hasNextInetSocketAddress()) {
     throw new SocketException(
         "No route to "
             + address.getUriHost()
             + "; exhausted inet socket addresses: "
             + inetSocketAddresses);
   }
   InetSocketAddress result = inetSocketAddresses.get(nextInetSocketAddressIndex++);
   resetConnectionSpecs();
   return result;
 }