How to upgrade and unlock iPhone 3gs?

I have an AT&T 3gs jail broken to accept my T-Mobile sim. It is running 4.1(8B117) on 06.15.00 firmware. Because the version it's running I can't download anything and Cydia as well as my network often don't work and I get poor reception in most places. What is the procedure to upgrade to the latest version (or one that is close to it) while still being able to use the T-Mobile Network?

T-mobile netword - ftw

You can update your 3gs all the way to iOS6, I believe you can update with iTunes, and then unlock with ultrasn0w.

I recommend you put Apple's firmware back on it and instead of using a jailbreak for your unlock you just get a factory unlock. If you get a factory unlock they are permanent and updates don't affect them because the unlock is on Apple's servers rather than on the phone itself. Lots of places do factory unlocking for a good price (i.e iphoneunlockedcheap.com)