Oddly enough, I touched on this a couple of days ago, although I'm happy to expand a bit more here. English reactions to opera (and particularity castrati) are one of my faaaaavorite things to bore people with.
Musical theater has possibly been associated with gay people since near to the beginning of opera, although we're going to have to get into some sticky issues of what is "gay culture," since gay identity didn't exist in the 18th century of course. And for the modern Broadway traditions there might be a different opinion from that side on when the link came about.
BUT, I can tell you there was an association between opera and sodomy (to use the terminology of the time) pretty strongly in English culture, and less strongly in Italian culture but still there I'd say. The influence of castrati in this link I think is rather under-emphasized (but then again, I am probably biased). Eunuchs were, for the time, something of the ultimate passive sex partner, as falling in the sexual models of the time as a permanent boy. Obviously preferable to real boys (because pederasty did happen back then) because they were mentally mature sex partners who could consent and not blab to the police for one.^1 Prominent men of the time (who might identify as gay or bisexual if they lived now) did take eunuch lovers, including notably Ferdinando de' Medici, Grand Prince of Tuscany, who had two known castrati lovers, and Card. Pietro Ottoboni who had Andrea Adami.
So basically opera had castrati, a sort of ultimate 18th century sex symbol, as a very prominent part of what opera is in both Italy and England (who only had opera as an "export product" for a long time), which is something that makes it associated with sodomy. This is not to say that castrati were not sex symbols for women, because they were, and back then you could be a gay and a straight sex symbol, since the division didn't exist of course. They sort of represented equal opportunity sexual sinning.
I've never seen anyone specifically argue that there is a link between the English reactions to opera in the 18th century and the current "gayness" of musical theater (including, to some extent, opera, but not as much as Broadway^2) but I'm arguing it now.
For more on the sexiness of castrati see The Eroticism of Emasculation: Confronting the Baroque Body of the Castrato or Freitas's book on Atto Melani which rehashes it and analyses Melani's sexuality specifically.
"Opera queens" is still a thing, there's a book on this called The Queen's Throat: Opera, Homosexuality, and the Mystery of Desire by Wayne Koestenbaum but it's kinda a punishing read, I wouldn't totally recommend it. But it does make a good argument for the "gayness" of opera in the America 1950s-now.
Let's start with the easier questions: when/where did musical theatre become "gay?" Homosexuality as an identity instead of an act (who you are, not what you do) has only been a Western concept since about the mid-19th century. Conicidentally, musical theatre has only been a concept since about the same time.
Unfortunately, because of the social stigma surrounding homosexuality, there aren't any contemporary accounts of homosexuality among actors in the Victorian age, and no diary has yet been discovered from a closeted gay actor. That said, there are contemporary accounts of groups of effeminate men attending a Gilbert and Sullivan operetta in London. They aren't overtly labelled as gay, but the descriptions imply that pretty heavily.
By the time American musical theatre was really coming into its own, in the first decade of the 20th century, actors- "chorus boys" especially- were already stereotyped as gay. There was a popular vaudeville skit "starring" two Schubert chorus boys, both of whom were portrayed as particularly flamboyant, so the audiences of the time were familiar with the cliché. (Ironically, Schubert hated gay men, but had no problem employing them.). The 1920's burlesque/vaudeville shows introduced the drag performer. Men had played women for years, but this was the first time it was done with a wink to the audience rather than the request for suspension of disbelief.
The next wave of Broadway was the quasi-closeted phase. Writers like Moss Hart and Cole Porter made no secret of being gay within the theatre community, but this was not widely known to their audiences. There were sly references to their homosexuality throughout their writing, most famously Cole Porter's double entendre "but if baby, I'm the bottom, you're the top."
There are consistent examples from there on: Irving Berlin wrote a show to be performed by servicemen, which attracted the gay enlisted men. It was almost shut down because of this, but was bringing in too much money for the war effort at that point. Ten years later, lyricist Alan Lerner famously said he avoided "s's" in his writing because they reminded audiences of the "lack of masculinity" in the cast.
So to answer the easy " when" question- since the beginning, it seems.
"Why" musical theatre became associated with gay culture is a) a question more for sociologists, and b) one with no clear answer. I've never heard the theory from /u/cafarelli before, but it seems logical. Other theories include Freudian interpretations of men seeing men dressed as women in the Renaissance theatre, and the fact that because actors traditionally were societal outcasts, the business attracted people who were societal outcasts anyway, and it became one place where gay men could find work.
Further reading: George Chauncey's "Gay New York"