@Override
    public void flush() throws IOException {
        try {
            // Calling flush but write has not been called. do nothing
            if (chunkIndex == 0 && position == 0) {
                return;
            }

            // Too many bytes written. Drop everything
            if (maxChunksReached) {
                return;
            }

            if (chunked) {
                flushChunked();

            } else {
                sendBytes(packetBytes, position);
            }

        } finally {
            reset();
        }

    }