# File lib/network/packetio.rb, line 43
  def read
    @inbuffer << @sock.recv(@bufsize)
    if !@prefix_found
      # start of packet
      if @inbuffer.size >= 4
        sizest = @inbuffer.slice!(0..3)
        @packet_size = sizest.unpack("N")[0]
        @prefix_found = true
        if @packet_size > @bufsize
          @inbuffer = ""
          @packet_size = 0
          @prefix_found = false
          log.warn "Discarding packet: Buffer size exceeded (PACKETSIZE=#{@packet_size} STRING='#{sizest}')"
          return nil
        end
      else
        return nil # not enough data yet
      end
    end

    if @prefix_found
      if @inbuffer.size >= @packet_size
        # We have it
        @prefix_found = false
        ps = @packet_size
        @packet_size = 0
        return @inbuffer.slice!(0...ps).chop  # chop off NUL
      else
        # Dont have it all yet.
        return nil
      end
    end
  end