anchor.diff:  	   end,
       lists:concat([S#state.url, "?", construct_headers(EReq, [])]).
   
  -handle_tracker_contact(S, Event) ->
  -    {ok, NextContactTime, NS} = contact_tracker(S, Event),
  -    install_timer(NextContactTime, NS).
  -
  -install_timer(ContactTime, S) ->
  -    if
  -	ContactTime > 30 ->
  -	    io:format("Setting 30 sec timer! (~B left)~n", [ContactTime]),
  -	    TimerRef = gen_fsm:start_timer(timer:seconds(30),
  -					   reinstall_timer),
  -	    S#state{timer = TimerRef,
  -		    time_left = ContactTime - 30};
  -	true ->
  -	    io:format("Setting timer of ~B seconds~n", [ContactTime]),
  -	    TimerRef = gen_fsm:start_timer(timer:seconds(ContactTime),
  -					   may_contact),
  -	    S#state{timer = TimerRef,
  -		    time_left = 0}
  -    end.
  -
  -handle_nonregular(S, Event) ->
  -    NS = remove_timer(S),
  -    handle_tracker_contact(NS, Event).
  -
  -remove_timer(S) ->
  -    T = S#state.timer,
  -    case T of
  -	none -> S;
  -	Ref ->
  -	    X = gen_fsm:cancel_timer(Ref),
  -	    io:format("Timer cancelled again: ~p~n", [X]),
  -	    S#state{timer = none,
  -		    time_left = none}
  -    end.
  -
   %%% Tracker response lookup functions
   find_next_request_time(BC) ->
       {integer, R} = bcoding:search_dict_default({string, "interval"},
  
positive.code: %%%-------------------------------------------------------------------
  %%% File    : tracker_delegate.erl
  %%% Author  : Jesper Louis Andersen <jlouis@succubus.local.domain>
  %%% Description : Controls the communication with a tracker.
  %%%
  %%% Created : 12 Jul 2007 by Jesper Louis Andersen <jlouis@succubus.local.domain>
  %%%-------------------------------------------------------------------
  -module(tracker_delegate).
  
  -behaviour(gen_fsm).
  
  %% API
  -export([start_link/5, contact_tracker_now/1, start_now/1, stop_now/1,
  	torrent_completed/1]).
  
  %% gen_fsm callbacks
  -export([init/1, tracker_wait/2,
  	 state_name/3, handle_event/3, handle_sync_event/4,
  	 handle_info/3, terminate/3, code_change/4]).
  
  -record(state, {should_contact_tracker = false,
  	        state_pid = none,
  
negative.code: %% API
  %%====================================================================
  %%--------------------------------------------------------------------
  %% Function: start_link() -> {ok,Pid} | ignore | {error,Error}
  %% Description: Starts the server
  %%--------------------------------------------------------------------
  start_link(ControlPid, StatePid, Url, InfoHash, PeerId) ->
      gen_server:start_link(?MODULE,
  			  [{ControlPid, StatePid, Url, InfoHash, PeerId}],
  			  []).
  
  contact_tracker_now(Pid) ->
      gen_server:cast(Pid, contact_tracker_now).
  
  start_now(Pid) ->
      gen_server:cast(Pid, start_now).
  
  stop_now(Pid) ->
      gen_server:cast(Pid, stop_now).
  
  torrent_completed(Pid) ->
      gen_server:cast(Pid, torrent_completed).
  
  %%====================================================================
  %% gen_server callbacks
  %%====================================================================
  
  %%--------------------------------------------------------------------
  %% Function: init(Args) -> {ok, State} |
  %%                         {ok, State, Timeout} |
  %%                         ignore               |
  %%                         {stop, Reason}
  %% Description: Initiates the server
  %%--------------------------------------------------------------------
  init([{ControlPid, StatePid, Url, InfoHash, PeerId}]) ->
      {ok, #state{should_contact_tracker = false,
  		control_pid = ControlPid,
  		state_pid = StatePid,
  		url = Url,
  		info_hash = InfoHash,
  		peer_id = PeerId}}.
  
  %%--------------------------------------------------------------------
  %% Function: %% handle_call(Request, From, State) -> {reply, Reply, State} |
  %%                                      {reply, Reply, State, Timeout} |
  %%                                      {noreply, State} |
  %%                                      {noreply, State, Timeout} |
  %%                                      {stop, Reason, Reply, State} |
  %%                                      {stop, Reason, State}
  %% Description: Handling call messages
  %%--------------------------------------------------------------------
  handle_call(_Request, _From, State) ->
      Reply = ok,
      {reply, Reply, State}.
  
  %%--------------------------------------------------------------------
  %% Function: handle_cast(Msg, State) -> {noreply, State} |
  %%                                      {noreply, State, Timeout} |
  %%                                      {stop, Reason, State}
  %% Description: Handling cast messages
  %%--------------------------------------------------------------------
  handle_cast(start_now, S) ->
      {ok, NextRequestTime, NS} = contact_tracker(S, "started"),
      {noreply, NS, timer:seconds(NextRequestTime)};
  handle_cast(stop_now, S) ->
      {ok, NextRequestTime, NS} = contact_tracker(S, "stopped"),
      {noreply, NS, timer:seconds(NextRequestTime)};
  handle_cast(torrent_completed, S) ->
      {ok, NextRequestTime, NS} = contact_tracker(S, "completed"),
      {noreply, NS, timer:seconds(NextRequestTime)}.
  
  %%--------------------------------------------------------------------
  %% Function: handle_info(Info, State) -> {noreply, State} |
  %%                                       {noreply, State, Timeout} |
  %%                                       {stop, Reason, State}
  %% Description: Handling all non call/cast messages
  %%--------------------------------------------------------------------
  handle_info(timeout, S) ->
      {ok, NextRequestTime, NS} = contact_tracker(S, none),
      {noreply, NS, timer:seconds(NextRequestTime)};
  handle_info(Info, State) ->
      io:format("got info: ~p~n", [Info]),
      {noreply, State}.
  
  %%--------------------------------------------------------------------
  %% Function: terminate(Reason, State) -> void()
  %% Description: This function is called by a gen_server when it is about to
  %% terminate. It should be the opposite of Module:init/1 and do any necessary
  %% cleaning up. When it returns, the gen_server terminates with Reason.
  %% The return value is ignored.
  %%--------------------------------------------------------------------
  terminate(Reason, _State) ->
      io:format("I am dying! ~p~n", [Reason]),
      ok.
  
  %%--------------------------------------------------------------------
  %% Func: code_change(OldVsn, State, Extra) -> {ok, NewState}
  %% Description: Convert process state when code is changed
  %%--------------------------------------------------------------------
  code_change(_OldVsn, State, _Extra) ->
      {ok, State}.
  
  %%--------------------------------------------------------------------
  %%% Internal functions
  