You must be connected to the internet before performing restore and use the latest version 4.0.1 and try to wait before itunes activate your iphone 3G. After that try to jb using redsn0w latest beta version and point it to version 4.0. Jailbreak will be ok and you can now use Ultrasn0w for unlock.