ConnectionHandler(SharedTorrent torrent, byte[] id, InetAddress address, boolean force)
		throws IOException {
		this.torrent = torrent;
		this.id = id;
		this.forceObfuscation = force;

		this.socket = new ServerSocket();

		// Bind to the first available port in the range
		// [PORT_RANGE_START; PORT_RANGE_END].
		for (int port = ConnectionHandler.PORT_RANGE_START;
				port <= ConnectionHandler.PORT_RANGE_END;
				port++) {
			InetSocketAddress tryAddress =
				new InetSocketAddress(address, port);

			try {
				this.socket.bind(tryAddress);
				this.address = tryAddress;
				break;
			} catch (IOException ioe) {
				// Ignore, try next port
				logger.info("Could not bind to " + tryAddress + "!");
			}
		}

		if (!this.socket.isBound()) {
			throw new IOException("No available port for BitTorrent client!");
		}

		this.listeners = new HashSet<IncomingConnectionListener>();
		this.thread = null;
	}