public static String getDNSName(ByteBuffer aBuffer) throws IOException {
    	StringBuffer buf = new StringBuffer();
        int next = -1;
        int first = aBuffer.position();

        int len = aBuffer.get();
        while(len != 0) {
        	switch(len & 0xC0) {
        	case 0x00:
        		// Top two bytes 00.  Indicates a label.
        		byte[] bytes = new byte[len];
        		aBuffer.get(bytes);
        		buf.append(s_Charset.decode(ByteBuffer.wrap(bytes)));
        		buf.append('.');
        		break;
        	case 0xC0:
        		// Top two bits 11 indicates a pointer
        		if(next < 0)
        			next = aBuffer.position() + 1;
        		// Strip off the top two bits and add in the next byte.
        		aBuffer.position( (len & 0x3F) << 8 | aBuffer.get() );
        		if(aBuffer.position() >= first)
        			throw new IOException("bad domain name: possible circular name detected");
        		first = aBuffer.position();
        		break;
        	default:
        		throw new IOException("Bad length on name!");	
        	}
        	
        	len = aBuffer.get();
        }
        
        // If we've been following pointers, reset to the original position where we jumped from
        if(next > 0)
        	aBuffer.position(next);
        
        return buf.toString();
    }