anchor.diff:  	    exit(normal)
       end.
   
  -do_cycle_port_program(Caller, Parent, Port, Cmd) ->
  +do_cycle_port_program(Caller, Parent, #state{port=Port} = S) ->
       unregister(?HEART_PORT_NAME),
       case catch start_portprogram() of
   	{ok, NewPort} ->
   	    _ = send_shutdown(Port),
   	    receive
   		{'EXIT', Port, _Reason} ->
  -		    _ = send_heart_cmd(NewPort, Cmd),
  +		    _ = send_heart_cmd(NewPort, S#state.cmd),
   		    Caller ! {heart, ok},
  -		    loop(Parent, NewPort, Cmd)
  +		    loop(Parent, S#state{port=NewPort})
   	    after
   		?CYCLE_TIMEOUT ->
   		    %% Huh! Two heart port programs running...
   		    %% well, the old one has to be sick not to respond
   		    %% so we'll settle for the new one...
  -		    _ = send_heart_cmd(NewPort, Cmd),
  +		    _ = send_heart_cmd(NewPort, S#state.cmd),
   		    Caller ! {heart, {error, stop_error}},
  -		    loop(Parent, NewPort, Cmd)
  +		    loop(Parent, S#state{port=NewPort})
   	    end;
   	no_heart ->
   	    Caller ! {heart, {error, no_heart}},
  -	    loop(Parent, Port, Cmd);
  +	    loop(Parent, S);
   	error ->
   	    Caller ! {heart, {error, start_error}},
  -	    loop(Parent, Port, Cmd)
  +	    loop(Parent, S)
       end.
       
   
  
positive.code: 		    _ = send_heart_cmd(Port, NewCmd),
  		    _ = wait_ack(Port),
  		    From ! {heart, ok},
  		    loop(Parent, Port, NewCmd);
  		_ ->
  		    From ! {heart, {error, {bad_cmd, NewCmd0}}},
  		    loop(Parent, Port, Cmd)
  	    end;
  	{From, clear_cmd} ->
  	    From ! {heart, ok},
  	    _ = send_heart_cmd(Port, ""),
  	    _ = wait_ack(Port),
  	    loop(Parent, Port, "");
  	{From, get_cmd} ->
  	    From ! {heart, get_heart_cmd(Port)},
  	    loop(Parent, Port, Cmd);
  	{From, cycle} ->
  	    %% Calls back to loop
  	    do_cycle_port_program(From, Parent, Port, Cmd);  
  	{'EXIT', Parent, shutdown} ->
  	    no_reboot_shutdown(Port);
  	{'EXIT', Parent, Reason} ->
  	    exit(Port, Reason),
  	    exit(Reason);
  	{'EXIT', Port, badsig} ->  % we can ignore badsig-messages!
  	    loop(Parent, Port, Cmd);
  	{'EXIT', Port, _Reason} ->
  	    exit({port_terminated, {heart, loop, [Parent, Port, Cmd]}});
  	_ -> 
  	    loop(Parent, Port, Cmd)
      after
  	?TIMEOUT ->
  	    loop(Parent, Port, Cmd)
      end.
  
  -spec no_reboot_shutdown(port()) -> no_return().
  
negative.code: -define(CYCLE_TIMEOUT, 10000).
  -define(HEART_PORT_NAME, heart_port).
  
  -record(state,{port :: port(),
                 cmd  :: [] | binary()}).
  
  %%---------------------------------------------------------------------
  
  -spec start() -> 'ignore' | {'error', term()} | {'ok', pid()}.
  