 
   prepareBuffer: ->
     @buffer = []
+    @indexedBuffer = {} if @isAddressable()
     @scopedBuffer = {}
+
+  prepareBufferForIP: (ip) ->
+    if @isAddressable()
+      if ip.scope?
+        @scopedBuffer[ip.scope] = [] unless ip.scope of @scopedBuffer
+        @scopedBuffer[ip.scope][ip.index] = [] unless ip.index of @scopedBuffer[ip.scope]
+        return @scopedBuffer[ip.scope][ip.index]
+      @indexedBuffer[ip.index] = [] unless ip.index of @indexedBuffer
+      return @indexedBuffer[ip.index]
+    if ip.scope?
+      @scopedBuffer[ip.scope] = [] unless ip.scope of @scopedBuffer
+      return @scopedBuffer[ip.scope]
+    return @buffer
 
   validateData: (data) ->
     return unless @options.values

    return buf

  # Fetches a packet from the port
  get: (scope) ->
    buf = @getBuffer scope
    return if @options.control then buf[buf.length - 1] else buf.shift()

  # Returns true if port contains packet(s) matching the validator
  has: (scope, validate) ->
    buf = @getBuffer scope
    return false unless buf?.length
    for packet in buf
      return true if validate packet
    false

  # Returns the number of data packets in an inport
  length: (scope) ->
    buf = @getBuffer scope
    return 0 unless buf
    return buf.length

  # Tells if buffer has packets or not
  ready: (scope) ->
    return @length(scope) > 0

  # Clears inport buffers
  clear: ->
    @buffer = [] if @buffer
    @scopedBuffer = {} if @scopedBuffer


module.exports = InPort

    return if @process
    return if @options.control and ip.type isnt 'data'
    ip.owner = @nodeInstance
    ip.index = id if @isAddressable()

    buf = @prepareBufferForIP ip
    buf.push ip
    buf.shift() if @options.control and buf.length > 1

