Wingnut's right - first thing to do is apologise. Tell him what you've just written here, if you like - he may need preparing for more insults! Your friends were good enough to point out to you what you're doing (it takes a very good friend to tell you you're making an idiot of yourself), which means you've got the self-awareness you need. Next thing is.. to stop doing it.
Thinking before you speak is a good start, but forcing yourself to be honest is also a good move. I would also guess you have shyness/self-esteem problems and that you don't necessarily truly believe the things you're saying - they're just a sort of defence mechanism, getting your retaliation in first. So stopping and thinking about how you really feel about what he's said, rather than just saying the first thing that jumps to your tongue, may help. It may slow your conversation down a bit, but I never knew a male who was put off a girl because she thought about what she said.
It might also help to force yourself to consider the feelings of people you speak to - ask yourself how they're going to feel if you say something silly or insulting. Is protecting your own feelings so important that you'll risk hurting theirs?
Looking back, what I've written sounds a bit harsh on you and I don't mean to be. Shyness isn't a crime. But sometimes it is self-reinforcing: you're shy, you say silly things, people are put off you, so you become shyer still. I think you'll be a happier person if you're less self-protective and more open to others' feelings.