public static final SocksImplementation getImplementation(
			final ConfigurationFacade configurationFacade, final Socket socket)
			throws IOException, AccessDeniedException, ProtocolException {
		final InputStream inputStream = socket.getInputStream();

		final int protocol = inputStream.read();

		switch (protocol) {
		case 0x04:
			if (configurationFacade.isAllowSocks4()) {
				return new SocksImplementation4(configurationFacade, socket,
						SocksImplementationFactory.EXECUTOR);
			} else {
				try {
					socket.close();
				} catch (final Exception e) {
				}
				throw new AccessDeniedException("SOCKS4 is not enabled");
			}

		case 0x05:
			if (configurationFacade.isAllowSocks5()) {
				return new SocksImplementation5(configurationFacade, socket,
						SocksImplementationFactory.EXECUTOR);
			} else {
				try {
					socket.close();
				} catch (final Exception e) {
				}
				throw new AccessDeniedException("SOCKS5 is not enabled");
			}

		default:
			try {
				socket.close();
			} catch (final Exception e) {
			}
			throw new ProtocolException("Unknown protocol: 0x"
					+ Integer.toHexString(protocol));
		}
	}