I saw that the to_contact is taken from the reply (iirc), maybe Santiago has more insights since they wrote the patch.
The reason why we implemented it that way was our lack of knowledge ... we just tried to do it the same way as the to-tag ... just getting it from the reply. I'm sure there are better ways to do it. Should we take it directly from the dialog structure?
Best regards,
Santi