@Override
    public void run() {
      try {
        long index = readIndex(socket);
        Excerpt excerpt = chronicle.createExcerpt();
        ByteBuffer bb = TcpUtil.createBuffer(1, chronicle); // minimum size
        while (!closed) {
          while (!excerpt.index(index)) pause(delayNS);
          int size = excerpt.capacity();
          int remaining = size + TcpUtil.HEADER_SIZE;

          bb.clear();
          bb.putLong(index);
          bb.putInt(size);
          while (remaining > 0) {
            int size2 = Math.min(remaining, bb.capacity());
            bb.limit(size2);
            excerpt.read(bb);
            bb.flip();
            remaining -= bb.remaining();
            IOTools.writeAll(socket, bb);
          }
          if (bb.remaining() > 0) throw new EOFException("Failed to send index=" + index);
          index++;
        }
      } catch (IOException e) {
        if (!closed) logger.log(Level.INFO, "Connect " + socket + " died", e);
      }
    }
 private long readIndex(SocketChannel socket) throws IOException {
   ByteBuffer bb = ByteBuffer.allocate(8);
   IOTools.readFullyOrEOF(socket, bb);
   return bb.getLong(0);
 }