If you have made a connection and are getting disconnected, then somewhere in your code, the connection is getting closed (unless your workstation is actually dropping the connection which I would doubt)

My suggestion would be to put a break at the beginning of the routine and "walk" through the entire thing to see exactly what is happening.