Where did the custom of removing your hat to show respect come from?

When men go indoors it is polite for them tor remove their hats. It is also customer to remove your hat to show pay respect at a funeral. Where did this custom come from? How did it start? I notice that if a woman has a hat with her outfit, this custom doesn't apply, why is that?
8 answers 8