private void authenticationExchange() throws IOException {
            // _console.writeline("authenticationExchange");

            int scheme = readCard32();
            if (scheme == 0) {
                String reason = readString();
                throw new VNCException("connection failed: " + reason);
            } else if (scheme == 1) {
                // no authentication needed
            } else if (scheme == 2) {
                try {
                    byte[] keyBytes = new byte[8];
                    for (int i = 0; (i < 8) && (i < password.length); ++i) {
                        keyBytes[i] = reverse((byte) password[i]);
                    }
                    Arrays.fill(password, '\0');
                    DESKeySpec keySpec = new DESKeySpec(keyBytes);
                    SecretKeyFactory keyFactory = SecretKeyFactory
                            .getInstance("DES");
                    SecretKey secretKey = keyFactory.generateSecret(keySpec);
                    Cipher cipher = Cipher.getInstance("DES");
                    cipher.init(Cipher.ENCRYPT_MODE, secretKey);
                    Arrays.fill(keyBytes, (byte) 0);
                    byte[] challenge = new byte[16];
                    readFully(challenge, 0, 16);
                    byte[] response = cipher.doFinal(challenge);
                    synchronized (out_) {
                        out_.write(response, 0, 16);
                        out_.flush();
                    }
                } catch (Exception e) {
                    throw new VNCException("authentication: DES error");
                }
                int status = readCard32();
                if (status == 0) {
                    // ok
                } else if (status == 1) {
                    throw new VNCException("authentication failed");
                } else if (status == 2) {
                    throw new VNCException("too many authentication failures");
                }
            } else {
                throw new VNCException("unexpected authentication scheme: "
                        + scheme);
            }
        }