anchor.diff:  	  mod = undefined,
   	  func = undefined,
   	  args = undefined,
  -	  buffer = undefined
  +	  pst = undefined %%  Payload state
   	 }).
   
   %%%===================================================================
  
positive.code: 		  mod = Mod,
  		  func = Fun,
  		  args = Arg,
  		  buffer = Buffer} = State) ->
      ?debug("~p:handle_info(data): Data: ~p", [ ?MODULE, Data]),
      ?debug("~p:handle_info(data): From: ~p:~p ", [ ?MODULE, IP, Port]),
  
      case count_brackets(Data, Buffer) of
  	{ incomplete, NBuffer } ->
  	    ?debug("~p:handle_info(data incomplete)", [ ?MODULE]),
  	    inet:setopts(Sock, [{active, once}]),
  	    {noreply, State#st { buffer = NBuffer} };
  
  	{complete, Processed, NBuffer } ->
  	    ?debug("~p:handle_info(data complete): Processed: ~p", [ ?MODULE, Processed]),
  	    FromPid = self(),
  	    spawn(fun() -> Mod:Fun(FromPid, IP, Port, 
  				   data, Processed, Arg) end),
  	    inet:setopts(Sock, [ { active, once } ]),
  	    {noreply, State#st { buffer = NBuffer} }
      end;
  
  
  
negative.code: %%% Internal functions
  %%%===================================================================
  
  
  count_brackets([${ | Rem], 
  	       #pst { 
  		  buffer = Buffer, 
  		  balance = start } = PSt)  ->
      count_brackets(Rem, 
  		   PSt#pst{ 
  		     buffer = [ ${ | Buffer ], 
  		     balance = 1});
  
  %% Drop any initial characters prior to opening bracket
  count_brackets([_ | Rem],
  	       #pst { balance = start } = PSt)  ->
      count_brackets(Rem, PSt );
  
  %% If balance is back to zero, we have completed a JSON
  %% element.
  count_brackets(Rem, 
  	       #pst { 
  		  buffer = Buffer, 
  		  balance = 0 } = PSt) ->
  
      { complete, lists:reverse(Buffer), 
        PSt#pst {
  	buffer = lists:reverse(Rem),
  	balance = start
         } 
      };
  
  %% If we still have balance, but no more input
  %% we have an incomplete element.x
  count_brackets([], PSt) ->
      { incomplete,  PSt };
  
  
  %% We have a string start or end, and we are not esacped
  %% Flip our in-string state
  count_brackets([$" | Rem],
  	       #pst {
  		  buffer = Buffer, 
  		  in_string = InString,
  		  escaped = false} = PSt) ->
  
      count_brackets(Rem, PSt#pst { 
  			  buffer = [ $" | Buffer ],
  			  in_string = not InString });
   
  
  %% We have an escape character, and we are in a string. Turn on our escape state
  count_brackets([$\\ | Rem],
  	       #pst { 
  		  buffer = Buffer,
  		  in_string = true,
  		  escaped = false } = PSt) ->
  
      count_brackets(Rem, PSt#pst { 
  			  buffer = [ $\\ | Buffer ],
  			  escaped = true});
   
  %% We have an opening bracket and we are not in a string
  count_brackets([${ | Rem], 
  	       #pst { 
  		  buffer = Buffer, 
  		  balance = Balance,
  		  in_string = false } = PSt) ->
  
      count_brackets(Rem,
  		   PSt#pst { 
  		     buffer = [ ${ | Buffer ], 
  		     balance = Balance + 1});
  
  %% We have an closing bracket and we are not in a string
  count_brackets([$} | Rem], 
  	       #pst { 
  		  buffer = Buffer, 
  		  balance = Balance,
  		  in_string = false } = PSt) ->
  
      count_brackets(Rem,
  		   PSt#pst { 
  		     buffer = [ $} | Buffer ], 
  		     balance = Balance - 1});
  
  %% We have just regular data to feed over.
  %% Make sure to clear the escape state.
  count_brackets([C | Rem],
  	       #pst { buffer = Buffer } = PSt) ->
  
      count_brackets(Rem, PSt#pst { 
  			  buffer = [ C | Buffer ],
  			  escaped = false
  			 } ).
  