I can't see the attachments, but I remember from your code you were doing
hAppserver = session.
This will screw you up. By doing this, you're connecting to the appserver, and then wiping the connection handle out with the current session handle, so you then try to run the .p locally from...