}
  public void connect()    throws IOException  {    if (connected)      {        return;      }    String protocol = url.getProtocol();    boolean secure = "https".equals(protocol);    String host = url.getHost();    int port = url.getPort();    if (port < 0)      {        port = secure ? HTTPConnection.HTTPS_PORT :          HTTPConnection.HTTP_PORT;      }    String file = url.getFile();    String username = url.getUserInfo();    String password = null;    if (username != null)      {        int ci = username.indexOf(':');        if (ci != -1)          {            password = username.substring(ci + 1);            username = username.substring(0, ci);          }      }    final Credentials creds = (username == null) ? null :      new Credentials (username, password);        boolean retry;    do      {        retry = false;        if (connection == null)          {            connection = getConnection(host, port, secure);            if (secure)              {                SSLSocketFactory factory = getSSLSocketFactory();                HostnameVerifier verifier = getHostnameVerifier();                if (factory != null)                  {                    connection.setSSLSocketFactory(factory);                  }                connection.addHandshakeCompletedListener(this);                // TODO verifier              }          }        if (proxyHostname != null)          {            if (proxyPort < 0)              {                proxyPort = secure ? HTTPConnection.HTTPS_PORT :                  HTTPConnection.HTTP_PORT;              }            connection.setProxy(proxyHostname, proxyPort);          }        request = connection.newRequest(method, file);        if (!keepAlive)          {            request.setHeader("Connection", "close");          }        if (agent != null)          {            request.setHeader("User-Agent", agent);          }        request.getHeaders().putAll(requestHeaders);        if (requestSink != null)          {            byte[] content = requestSink.toByteArray();            RequestBodyWriter writer = new ByteArrayRequestBodyWriter(content);            request.setRequestBodyWriter(writer);          }        ByteArrayResponseBodyReader reader = new ByteArrayResponseBodyReader();        request.setResponseBodyReader(reader);        if (creds != null)          {            request.setAuthenticator(new Authenticator() {              public Credentials getCredentials(String realm, int attempts)              {                return (attempts < 2) ? creds : null;              }            });          }        response = request.dispatch();        if (response.getCodeClass() == 3 && getInstanceFollowRedirects())          {            // Follow redirect            String location = response.getHeader("Location");            String connectionUri = connection.getURI();            int start = connectionUri.length();            if (location.startsWith(connectionUri) &&                location.charAt(start) == '/')              {                file = location.substring(start);                retry = true;              }            else if (location.startsWith("http:"))              {                connection.close();                connection = null;                secure = false;                start = 7;                int end = location.indexOf('/', start);                host = location.substring(start, end);                int ci = host.lastIndexOf(':');                if (ci != -1)                  {                    port = Integer.parseInt(host.substring (ci + 1));                    host = host.substring(0, ci);                  }                else                  {                    port = HTTPConnection.HTTP_PORT;                  }                file = location.substring(end);                retry = true;              }            else if (location.startsWith("https:"))              {                connection.close();                connection = null;                secure = true;                start = 8;                int end = location.indexOf('/', start);                host = location.substring(start, end);                int ci = host.lastIndexOf(':');                if (ci != -1)                  {                    port = Integer.parseInt(host.substring (ci + 1));                    host = host.substring(0, ci);                  }                else                  {                    port = HTTPConnection.HTTPS_PORT;                  }                file = location.substring(end);                retry = true;              }	    else if (location.length() > 0)	      {		// Malformed absolute URI, treat as file part of URI		if (location.charAt(0) == '/')		  {		    // Absolute path		    file = location;		  }		else		  {		    // Relative path		    int lsi = file.lastIndexOf('/');		    file = (lsi == -1) ? "/" : file.substring(0, lsi + 1);		    file += location;		  }		retry = true;	      }          }        else          {            responseSink = new ByteArrayInputStream(reader.toByteArray ());            if (response.getCode() == 404)              {                errorSink = responseSink;                throw new FileNotFoundException(url.toString());              }          }      }    while (retry);    connected = true;  }