@Override
	public long write(be.nabu.utils.io.api.ByteBuffer target) throws IOException {
		long length = target.remainingData();
		// the connect has to be done
		if (connect != null && !connect.isDone())
			return 0;
		// if we have a write pending, stop
		if (write != null && !write.isDone())
			return 0;
		// or if a write was done, remove from the buffer what was written
		else if (write != null && write.isDone()) {
			try {
				int amountWritten = write.get();
				// if nothing was written, stop
				if (amountWritten == 0)
					return 0;
				// otherwise clear the writeBuffer from what has been written already
				else
					writeBuffer.skip(amountWritten);
			}
			catch (InterruptedException e) {
				throw new RuntimeException(e);
			}
			catch (ExecutionException e) {
				throw new RuntimeException(e);
			}
		}
		// if we get here, write data to the buffer and initiate a write
		writeBuffer.write(target);
		this.write = socket.write(ByteBuffer.wrap(IOUtils.peekBytes(writeBuffer)));
//		writeBuffer.write(bytes, offset, length);
		return length;
	}