Hello.
I have tried your code and it´s working for me (with required adjustment to our System landscape).
However, this type of session Connection use an UI with restricted views and Options.
So I recommend to adopt to existing sessions created via SAP GUI application. As they will give same Option as you have via SAP GUI.
Br, Holger