There is no good way to lose a war. By definition, wars can be managed only after they are won.
It's a theocracy. We invaded them, primarily, to play off 9/11, politically (we could have hunted down terrorists and dealt with them, covertly). When you fight theocrats for political reasons, they are far more motivated than you. Arrogance prevented us from recognizing that in a timely way.