From Thomas Malthus, one of the first economists and sociologists.

When a population becomes too large to let things run smoothly, Malthusian checks begin to kick in. These are factors that put downward pressure on population levels. For lemmings, this means jumping off cliffs. For humans, it means killing each other (a very strange behavior for any animal), disease, prostitution (meaning fewer births without sexual frustration overheat), and even, Malthus argued, homosexuality.

In malthus' view, many of these checks evolved, and are built in to human society and genetics. Homosexuality, for instance, is beneficial in a society where more births would be harmful and not beneficial. And lo and behold, it tends to flourish when it is needed most. (Whether this is genetic or cultural is a can of worms I don't want to get into...)