Gender qualification for a job

Can gender ever be used legally as a required qualification for a job? If not, why? Is so, when?