public static void main(String[] args) {
		ServerSocketChannel serverChannel;
		Selector selector;

		try{
			serverChannel = ServerSocketChannel.open();
			ServerSocket ss = serverChannel.socket();
			InetSocketAddress addr = new InetSocketAddress(DEFAULT_PORT);
			ss.bind(addr);
			serverChannel.configureBlocking(false);
			selector = Selector.open();
			serverChannel.register(selector, SelectionKey.OP_ACCEPT);
		}catch(IOException ex){
			ex.printStackTrace();
			return;
		}

		while(true){
			try{
				selector.select();
			}catch(IOException ex){
				ex.printStackTrace();
				break;
			}

			Set<SelectionKey> readyKeys = selector.selectedKeys();
			Iterator<SelectionKey> iterator = readyKeys.iterator();

			while(iterator.hasNext()){
				SelectionKey key = iterator.next();
				iterator.remove();

				try{
					if(key.isAcceptable()){
						ServerSocketChannel server =
								(ServerSocketChannel) key.channel();
						SocketChannel client = server.accept();
						System.out.println("Accepted client from : " + client);
						client.configureBlocking(false);
						SelectionKey key2 = client.register(
								selector, SelectionKey.OP_WRITE);
						ByteBuffer output = ByteBuffer.allocate(4);
						output.putInt(0);
						output.flip();
						key2.attach(output);
					}else if(key.isWritable()){
						SocketChannel client = (SocketChannel) key.channel();
						ByteBuffer output = (ByteBuffer)key.attachment();
						if(!output.hasRemaining()){
							output.rewind();
							int value = output.getInt();
							output.clear();
							output.putInt(value + 1);
							output.flip();
						}
						client.write(output);
					}
				}catch(IOException ex){
					key.cancel();
					try{
						key.channel().close();
					}catch(IOException exc){
						exc.printStackTrace();
					}
				}
			}
		}
	}