first flash your iphone 3gs 4.0 with itunes 9.2 and after finished uninstall itunes 9.2 and install itunes 9.0 then flash it with 3.1.3,at the end of procedure will appear an error but doesn't matter anything just use ireb and everything is fine,dont forget you will need original sim or these universal sim to activate it and than unlock it with ultrasnow...........dont forget to do this in case you cant flash with 3.1.3
http://forum.gsmhosting.com/vbb/f456/iphone-3gs-4-0-downgraded-3-1-3-successfully-jailbreak-unlocked-1030943/index5.html