Islam's approach to homosexuality has been in the spotlight since the massacre at an Orlando gay club — criminal or compassionate? Prejudiced or progressive?
While ISIS death squads enforce an extreme version of Islam that punishes gays with death, the religion's history is far more nuanced. And like most relationships, when it comes to Islam and homosexuality — it's complicated.
Experts say that LGBT figures have been a feature of the faith since the days of the Prophet Muhammad.
"There is sexual diversity in Islam," said Ali A. Olomi, an Afghan-American activist and historian who teaches at the University of California in Irvine. "Of course there is homosexuality, gay people have existed and even been represented in Islam throughout history."
Most scholars agree that musician Al Dalal, a contemporary of the Prophet Muhammad, was gay. Then there's the 13th Century Persian poet Rumi, who wrote homoerotic verses dedicated to same-sex love.
Still, Islamic scholars overwhelmingly teach that same-gender sex is a sin. The Muslim holy book, the Quran, tells the story of Lot and the destruction of Sodom — with sodomy in Arabic referred to as "liwat," based on Lot's name.
Men having sex with each other should be punished, the Quran says, but it doesn't say how — and it adds that they should be left alone if they repent.
Gay Muslims say the Quran is not as prescriptive as many imams suggest.
"The Quran says little about homosexuality and many claims are made about the content of the Quran that do not necessarily stand scrutiny," according to Imaan, a British LGBT support group.
A small number of Islamic scholars, mainly in the West, have started re-examining Islamic teaching on same-gender sex and have concluded that the blanket condemnation is a misinterpretation.
Historic texts on Islam "deal with violence against men but not love or relations between women or any kind of nuance," said Olomi. "When you try to put ancient interpretations of sodomy onto modern sexuality it becomes convoluted.
"Religious leaders who say homosexuality is anti-Islamic interpret Islam in a narrow fashion that erases thousands of years of diversity and complexity," Olomi added. "There isn't a singular, modular concept of sexuality in Islam, it is always been debated and contested."
The review of Islamic teaching on same-gender sex, though, is only just beginning and is not widely accepted.
The idea of the death penalty comes not from the Quran but the Hadith — the traditions or sayings of the Prophet Muhammad. Accounts differ on the method of killing, and some versions give lesser penalties in some circumstances.
The Morning Rundown
Let our news meet your inbox. The news and stories that matters, delivered weekday mornings.
The death penalty is imposed in at least 10 countries: Afghanistan — where the Orlando shooter's father was from — Brunei, Iran, Mauritania, Saudi Arabia, parts of Somalia, Sudan, United Arab Emirates and Yemen.
In those countries "even the existence of homosexuality is only grudgingly acknowledged," according to Afghan-American writer Fariba Nawa.
Iran is notorious for hanging men accused of homosexual behavior. The Associated Press reports that since 2014, ISIS has executed at least 30 people in Syria and Iraq for being gay — including three men who were dropped from the top of a 100-foot building in Mosul in June 2015.
During their rule in Afghanistan in the 1990s, the Taliban had their own method: The victim would be placed in a pit and a large stone wall toppled on top of him.
Experts say the Orlando shooting has exposed hypocrisy in countries' treatment of homosexuality.
"Middle Eastern and North African countries have denounced the Orlando shooting when at the same time they criminalize homosexuality with sentences ranging from years in prison to the death penalty," said Ahmed Benchemsi, communications and advocacy director for the Middle East and North Africa at Human Rights Watch. "Those governments should repeal laws and abolish practices that persecute people based on their sexual orientation and gender identity."
Afghan president Ashraf Ghani tweeted that he condemned the "heinous and unforgivable crime in Orlando."
Another Twitter user quickly pointed out: "If you really think this crime towards LGBT people was bad, stop killing them for having same sex activity in your country!"
No fewer than 40 out of 57 Muslim-majority countries or territories have laws that criminalize homosexuality, prescribing punishments ranging from fines and short jail sentences to whippings.
However, same-sex relationships are not strictly illegal in 20 Muslim-majority nations including Albania, Bosnia and Herzegovina, Turkey, Kosovo, Jordan, and the Palestinian West Bank.
In Egypt, there have been police raids of suspected gay gatherings and people have been put on trial for "violating public morality."
Religiously-mixed Lebanon is the most liberal among Arab nations regarding same-sex relationships, and has an active LGBT community. A Lebanese law forbids homosexuality, although it is rarely enforced and has been challenged in courts.
There has been plenty of support for LGBT rights among Muslims. Just over 40 percent of U.S. Muslims said they supported same-sex marriage last year in a survey by the Washington-based Public Religion Research Institute. Lawmakers in Albania recently debated legalizing gay marriage.
In fact, Muslims in the U.S. are more accepting of homosexuality than evangelical Christians, Mormons and Jehovah's Witnesses, according to a 2015 Pew study.
Community leaders acknowledge that homophobia remains a problem, but there is a debate about whether that comes down to specific Islamic teaching on homosexuality or socially conservative attitudes.
"Sexuality in general is a somewhat taboo topic. The idea that homosexuality may exist is not something often discussed," Olomi explained. He added that homophobia was "not unique" to Islam but "quite a serious problem" in the U.S.
While there are signs Muslim communities in the U.S. and other Western nations are adapting to different social norms, the pendulum is swinging the other way in countries such as Iraq.
The rise of modern Islamic extremism has worsened homophobia in Muslim societies outside of the U.S., according to Ayaan Hirsi Ali, author of "Infidel" a fellow at the Harvard Kennedy School.
Others see signs of progress. Ibi Ibrahim, a Yemeni artist who has lived in the U.S., says "changes are happening, slowly" in the Middle East.
"Just a week ago I was reading a Moroccan newspaper and the front page was about a gay Moroccan man who did something in Europe and it was actually positive, the article was written in a positive way," Ibrahim said at a recent London exhibition on sexuality in Islam called "The Unbreakable Rope."
"I never thought I would just open the page of an Arab newspaper and see that being discussed in a normal way," he added.
Many gays in the Muslim world keep their sexual orientation secret for fear of reprisal by relatives or authorities, while some find a surprising degree of acceptance.
"Like many other gay men, coming out for me was certainly nerve-racking but in the end was the best decision I made," said a Bay Area financial analyst and Afghan-American who did not want to be identified. "The response I received from my parents them was nothing short of unconditional love and support. They played a vital role in helping me fully accept myself."
He acknowledged that other Muslims, even in the U.S., have not been so lucky.
"I have met other Afghans and Muslims that have been ostracized by their families, or live in the closet for fear of being disowned," he said.
Nemat Sadat, 37, a New York City-based writer, faced death threats and was shunned when he came out in 2013. He is now an avowed atheist and a campaigner for LGBT rights in Afghanistan.
"Even if it takes me 50 years, this is what I am committed to," he said. "Even if I end up getting killed in the process, this is what I what to achieve."
Alastair Jamieson is a London-based reporter, editor and homepage producer for NBC News.
Michele Neubert is a London-based producer for NBC News. She has been awarded four Emmys, an Ed Murrow and Dupont Award for her work in conflict zones, including the Balkans, Afghanistan and Kurdistan.