public void run() {

	try {
	    Selector selector = Selector.open();
	    ServerSocketChannel server_channel = ServerSocketChannel.open();
	    server_channel.configureBlocking(false);
	    server_channel.socket().bind(new InetSocketAddress("localhost", this.port));
	    server_channel.register(selector, SelectionKey.OP_ACCEPT);	    
	    while (run) {
		selector.select();
		Iterator<SelectionKey> keys = selector.selectedKeys().iterator();
		while (keys.hasNext() && run) {
		    SelectionKey key = keys.next();
		    keys.remove();
		    if (!key.isValid()) {
			continue;
		    }
		    if (key.isAcceptable()) {
			IT.log("Accepting local connection...");			
			ServerSocketChannel key_server_channel = (ServerSocketChannel) key.channel();
			SocketChannel channel = key_server_channel.accept();
			channel.configureBlocking(false);
			SelectionKey nkey = channel.register(key.selector(), SelectionKey.OP_WRITE);
			Handler handler = new Handler(nkey, mutex);
			nkey.attach(handler);
			/**
			 * After accepting a new connection put it in wait for the lock.
			 */
			mutex.lock(handler);
		    } else if (key.isReadable()) {
			Handler handler = (Handler) key.attachment();
			handler.read(key);
		    } else if (key.isWritable()) {
			Handler handler = (Handler) key.attachment();
			handler.write(key);
		    }
		}
	    }
	} catch (IOException e) {
	    e.printStackTrace();
	}
    }