On a recent Friday, gay and lesbian couples, dressed in matching outfits, posed for photos outside of a Nairobi courthouse, in anticipation of a decision that they hoped would decriminalize gay sex in Kenya. The country’s High Court, however, unexpectedly postponed, until May 24th, its ruling on whether to strike down a nearly century-old law. “Whatever happens, we intend to fight this battle until the end,” Njeri Gateru, the director of the National Gay & Lesbian Human Rights Commission, one of the gay-rights groups litigating the case, told me. “The end, to us, means a space where we finally achieve equality for all people, regardless of their sexual orientation or gender identity.”

Introduced by Britain during colonial rule and incorporated into Kenyan law after the country gained independence, in 1963, the penal code punishes acts “against the order of nature”—usually interpreted as sex between men—with up to fourteen years in prison. It also prescribes up to five years in prison for “gross indecency with another male person,” which is often interpreted as other, undefined sexual acts between men. Worldwide, at least seventy nations—more than a third of all countries—still outlaw homosexuality, and it remains illegal in more than thirty of the fifty-four African countries.

After Kenya’s independence, these codes appear to have gone largely unenforced. For decades, homosexuality wasn’t widely talked about, Gateru told me. In the nineteen-nineties, when she was growing up in northeastern Kenya, “there was no name for a gay person,” she said. “There was also no name for a heterosexual. There was no separation. Nobody was speaking about it at all—there weren’t even the words for it.”

By the late two-thousands, religious leaders across East Africa had begun publicly denouncing homosexuality—sometimes with the encouragement of American missionaries. According to a Pew survey in 2013, ninety per cent of Kenyan respondents said that society should not accept homosexuality. Since homosexuality remains illegal under the penal code, family members and neighbors sometimes report suspected homosexuals to the police. The Kenyan government claims that, between 2010 and 2014, nearly six hundred people were criminally investigated under the unnatural-offenses penal code. People who are seen as “nonconforming,” Gateru said, are most often those publicly attacked.

In 2016, L.G.B.T. activists looking for a way to curb discrimination began targeting the laws that criminalized homosexuality. Changing a society’s values would take generations, they reasoned, but striking down an unjust law could be accomplished in just a few years. “The only way we were going to decriminalize homosexuality was through the courts,” Gateru told me. “It was time to test the waters.”

Recent landmark rulings by the country’s High Court and Supreme Court on issues of human rights and democracy have spurred hope that the judiciary will extend legal protections to L.G.B.T. people. “The judicial system in Kenya has shown a lot of independence. We saw them nullify an election,” Kari Mugo, a former colleague of Gateru’s, told me, in reference to the Supreme Court’s decision, in 2017, to annul a contested Presidential race and order a new vote. “That has never happened before on this continent.”

In addition, Kenya recently enacted a new constitution, which gave L.G.B.T. advocates the legal strategy they needed to challenge the penal code in court. “The 2010 constitution was very pivotal,” Gateru said, because “it has a very comprehensive bill of rights. It speaks particularly to the protection of specific groups, marginalized groups. And it allows for public-interest litigation—to sue on others’ behalf.”

The new constitution allows activists to challenge homophobic laws in court without requiring them to prove that a particular individual was denied a job or an inheritance because of a discriminatory law. Instead, plaintiffs need only to persuade the court that a given law, as written, is inherently discriminatory. That is what the current case before the High Court is designed to do. Advocates hope that the judges will declare the law criminalizing homosexuality unconstitutional because it is intrinsically discriminatory.

In 2015, the High Court ruled that the N.G.L.H.R.C. could register with Kenya’s N.G.O. board with the words “gay” and “lesbian” in its name, the first of several legal victories for L.G.B.T.-rights advocates. Last year, an appeals court in the port city of Mombasa ruled that forcing two men to undergo anal examinations to determine if they had had gay sex “was not only unconstitutional but unreasonable, and totally unnecessary.” In its decision, the three-judge panel quoted the Kenyan Constitution’s assertion that “the purpose of recognizing and protecting human rights and fundamental freedoms is to preserve the dignity of individuals and communities.”

That ruling “sent a very powerful statement,” Mugo told me. It signalled that some judges might be amenable to rejecting other forms of legal discrimination—and perhaps even the anti-gay penal code itself. In another ruling, in September, the High Court rejected a ban, by the Kenya Film Classification Board, of the film “Rafiki,” which portrays a lesbian love story. “Looking at those decisions gives us hope that the court can stand on its own, even in the face of popular opinion on these matters,” Mugo said.

Even if the court invalidates the sections of the penal code that criminalize gay sex, popular opinion in the country remains generally hostile toward the L.G.B.T. community. In a 2016 survey, forty-nine per cent of Kenyans polled said that they strongly opposed same-sex relationships, and forty per cent said they strongly agreed that being homosexual should be a crime. Many politicians, including Deputy President William Ruto, have publicly condemned gay people, and, in 2014, members of Parliament proposed and failed to pass a bill that would have increased the maximum penalty for engaging in homosexual acts, from fourteen years in prison to a life sentence.

When I arrived in East Africa, in 2013, stories of workplace discrimination and firings, blackmail and extortion, and muggings and beatings of L.G.B.T. people were common. Across the region, people have used laws against “unnatural offenses” to blackmail L.G.B.T. people, threatening to out them unless they provide money or, in some cases, sex. People who have been arrested and charged with homosexual activity complain that police have coerced confessions from them or demanded bribes in order to drop the charges.

Opponents of homosexuality argue that it is not a natural circumstance but, rather, an ideology that foreigners are attempting to impose on Africans, particularly the youth. In the past decade, however, as news outlets have reported on the issue of homosexuality, Kenyans have seen queer people on television and read about them in newspapers, putting human faces to the community for the first time. “Without this visibility, we’d be asking judges to decide on something that was too abstract,” Gateru told me. “Now, if one of those learned judges Googled ‘homosexuality in Kenya,’ there would be so much.”

Many church and political groups, though, remain opposed to gay rights. In all of these recent court cases, the Kenya Christian Professional Forum has argued against legal motions filed by L.G.B.T. activists. The Nairobi-based organization’s stated mission is “to promote and defend biblical values in society” and “to influence value-based legislation, policy and governance.” “The penal code has crimes against robbery, crimes against murder, crimes against things that are harmful to society. It’s not just homosexuality,” Charles Kanjama, a lawyer representing the K.C.P.F., told me. He said, “It’s unfortunate that . . . people who want to accomplish social change prefer the courts as their battleground as opposed to the legislature. The courts are not good places for dealing with policy.”