anchor.diff:  e(_, #diameter_packet{msg = [#diameter_header{} = Hdr | As]} = Pkt) ->
       try encode_avps(reorder(As)) of
           Avps ->
  -            Length = size(Avps) + 20,
  +            Len = 20 + size(Avps),
   
               #diameter_header{version = Vsn,
  +                             is_request = R,
  +                             is_proxiable = P,
  +                             is_error = E,
  +                             is_retransmitted = T,
                                cmd_code = Code,
                                application_id = Aid,
                                hop_by_hop_id  = Hid,
                                end_to_end_id  = Eid}
                   = Hdr,
   
  -            Flags = make_flags(0, Hdr),
  -
               Pkt#diameter_packet{header = Hdr,
  -                                bin = <<Vsn:8, Length:24,
  -                                        Flags:8, Code:24,
  +                                bin = <<Vsn:8, Len:24,
  +                                        ?FLAGS(R,P,E,T), Code:24,
                                           Aid:32,
                                           Hid:32,
                                           Eid:32,
  
positive.code: msg_id(#diameter_header{application_id = A,
                          cmd_code = C,
                          is_request = R}) ->
      {A, C, if R -> 1; true -> 0 end};
  
  msg_id(<<_:32, Rbit:1, _:7, CmdCode:24, ApplId:32, _/binary>>) ->
      {ApplId, CmdCode, Rbit}.
  
negative.code:     end;
  
  e(Mod, #diameter_packet{header = Hdr0, msg = Msg} = Pkt) ->
      MsgName = rec2msg(Mod, Msg),
      {Code, Flags, Aid} = msg_header(Mod, MsgName, Hdr0),
  
      #diameter_header{version = Vsn,
                       is_request = R,
                       is_proxiable = P,
                       is_error = E,
                       is_retransmitted = T,
                       hop_by_hop_id = Hid,
                       end_to_end_id = Eid}
          = Hdr0,
  
      RB = ?FLAG(R, Flags band 2#10000000),
      PB = ?FLAG(P, Flags band 2#01000000),
      EB = ?FLAG(E, Flags band 2#00100000),
      TB = ?FLAG(T, Flags band 2#00010000),
  
      Values = values(Msg),
  
      try encode_avps(Mod, MsgName, Values) of
          Avps ->
              Len = size(Avps) + 20,
              Hdr = Hdr0#diameter_header{length = Len,
                                         cmd_code = Code,
                                         application_id = Aid,
                                         is_request       = RB,
                                         is_proxiable     = PB,
                                         is_error         = EB,
                                         is_retransmitted = TB},
              Pkt#diameter_packet{header = Hdr,
                                  bin = <<Vsn:8, Len:24,
                                          ?FLAGS(RB, PB, EB, TB), Code:24,
                                          Aid:32,
                                          Hid:32,
                                          Eid:32,
                                          Avps/binary>>}
      catch
          error: Reason ->
              Hdr = Hdr0#diameter_header{cmd_code = Code,
                                         application_id = Aid,
                                         is_request       = RB,
                                         is_proxiable     = PB,
                                         is_error         = EB,
                                         is_retransmitted = TB},
              exit({Reason, diameter_lib:get_stacktrace(), Hdr})
      end.
  
  %% values/1
  
  