How to write a SIP attended transfer application

There are two ways of invoking an attended transfer using SIP: