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:     end;
  
  e(Mod, #diameter_packet{header = Hdr0, msg = Msg} = Pkt) ->
      #diameter_header{version = Vsn,
                       hop_by_hop_id = Hid,
                       end_to_end_id = Eid}
          = Hdr0,
  
      MsgName = rec2msg(Mod, Msg),
      {Code, Flags0, Aid} = msg_header(Mod, MsgName, Hdr0),
      Flags = make_flags(Flags0, Hdr0),
      Hdr = Hdr0#diameter_header{cmd_code = Code,
                                 application_id = Aid,
                                 is_request       = 0 /= ?MASK(7, Flags),
                                 is_proxiable     = 0 /= ?MASK(6, Flags),
                                 is_error         = 0 /= ?MASK(5, Flags),
                                 is_retransmitted = 0 /= ?MASK(4, Flags)},
      Values = values(Msg),
  
      try encode_avps(Mod, MsgName, Values) of
          Avps ->
              Length = size(Avps) + 20,
              Pkt#diameter_packet{header = Hdr#diameter_header{length = Length},
                                  bin = <<Vsn:8, Length:24,
                                          Flags:8, Code:24,
                                          Aid:32,
                                          Hid:32,
                                          Eid:32,
                                          Avps/binary>>}
      catch
          error: Reason ->
              exit({Reason, diameter_lib:get_stacktrace(), Hdr})
      end.
  
  %% make_flags/2
  
  make_flags(Flags0, #diameter_header{is_request       = R,
                                      is_proxiable     = P,
                                      is_error         = E,
                                      is_retransmitted = T}) ->
      {Flags, 3} = lists:foldl(fun(B,{F,N}) -> {mf(B,F,N), N-1} end,
                               {Flags0, 7},
                               [R,P,E,T]),
      Flags.
  
  mf(undefined, F, _) ->
      F;
  mf(B, F, N) ->  %% reset the affected bit
      (F bxor (F band (1 bsl N))) bor bit(B, N).
  
  bit(true, N)  -> 1 bsl N;
  bit(false, _) -> 0.
  
  %% values/1
  
  
negative.code: msg_id(#diameter_header{application_id = A,
                          cmd_code = C,
                          is_request = R}) ->
      {A, C, ?BIT(R)};
  
  msg_id(<<_:32, Rbit:1, _:7, CmdCode:24, ApplId:32, _/binary>>) ->
      {ApplId, CmdCode, Rbit}.
  