class EventMachine::Synchrony::TCPSocket

def try_read_data

def try_read_data
  if @read_type == :read || @read_type == :read_nonblock
    nonblocking = @read_type == :read_nonblock
    unless @remote_closed
      if @read_bytes
        # read(n) on an open socket, with >= than n buffered data, returns n data
        if (@in_buff.size >= @read_bytes ||
            (nonblocking && @in_buff.size > 0)) then
          @in_buff.slice!(0, @read_bytes)
        # read(n) on an open socket, with < than n buffered data, blocks
        else :block end
      else
        # read() on an open socket, blocks until a remote close and returns all the data sent
        :block
      end
    else
      if @read_bytes
        # read(n) on a remotely closed socket, with no buffered data, returns nil
        if @in_buff.empty? then nil
        # read(n) on a remotely closed socket, with buffered data, returns the buffered data up to n
        else @in_buff.slice!(0, @read_bytes) end
      else
        # read() on a remotely closed socket, with no buffered data, returns ""
        if @in_buff.empty? then ""
        # read() on a remotely closed socket, with buffered data, returns the buffered data
        else @in_buff.slice!(0, @in_buff.size) end
      end
    end
  else #recv
    unless @remote_closed
      # recv(n) on an open socket, with no buffered data, blocks
      if @in_buff.empty? then :block
      # recv(n) on an open socket, with < than n buffered data, return the buffered data
      # recv(n) on an open socket, with >= than n buffered data, returns n data
      else @in_buff.slice!(0, @read_bytes) end
    else
      # recv(n) on a remotely closed socket, with no buffered data, returns ""
      if @in_buff.empty? then ""
      # recv(n) on a remotely closed socket, with < than n buffered data, return the buffered data
      # recv(n) on a remotely closed socket, with >= than n buffered data, returns n data              
      else @in_buff.slice!(0, @read_bytes) end              
    end
  end
end