public void write(final ProtocolEncoder encoder) throws Exception
    {
		final StringBuilder log = logger.isLoggable(Level.FINER) ? new StringBuilder() : null;
		buffer.clear();
		encoder.encode(buffer, log);
		if (log != null)
			logger.finer("Protocol Encoding\n" + log.toString());
		
		buffer.flip();
		if (logger.isLoggable(Level.FINEST))
			logger.log(Level.FINEST, "Data sent ({0} bytes):\n{1}",
					new Object[] { buffer.remaining(), Hexdump.toHexdump(buffer) });
		
		int to_write = buffer.limit();
		while (to_write > 0)
		{
			final int written = channel.write(buffer).get(timeout_ms, MILLISECONDS);
			to_write -= written;
			if (to_write > 0)
				buffer.compact();
		}
    }