public void connect() throws IOException {
        if (level != 0) // otherwise disconnected or connect
            throw new SocketException("Socket closed or already open (" + level + ")");
        IOException exception = null;
        Socket s = null;
        for (int i = 0; i < ports.length && s == null; i++) {
            try {
                s = new Socket(host, ports[i]);
                exception = null;
            }
            catch (IOException exc) {
                exception = exc;
            }
        }
        if (exception != null)
            throw exception; // connection wasn't successful at any port

        prepare(s);
    }