A British man flew home from Marrakech last week after being jailed for "homosexual acts". There was a time though when Morocco was renowned as a haven for gay Americans and Britons, who fled restrictions in their own countries to take advantage of its relaxed atmosphere.

Take a walk down one of the main streets in Tangier, the Boulevard Pasteur, turn left before the Hotel Rembrandt and descend towards the sea. Then follow some steps into a narrow side street that smells of urine and screams of danger.

Overlooking an empty space that looks like a disused car park or the aftermath of a nuclear bomb, is a family-run hostel called El Muniria, a white block with blue windowsills and a crenelated roof.

It was here in Room 9, in the 1950s, that William Burroughs, high on drugs, wrote one of the 20th Century's most shocking novels, Naked Lunch. The book, banned under US obscenity laws, is a mixture of autobiography, science fiction and satire, peppered with descriptions of gay sex.

Image copyrightBBC Richard Hamilton

When I enter the Muniria, the youngest member of the family tells me that I can look around, but that Room 9 is locked, as his uncle has "gone away with the key."

The corridors are desolate with some mould on the walls. A black and white portrait of Burroughs in hat and dark glasses stares blankly back above a rubber plant. The bathroom is bleak, like the inside of an asylum, with white tiles everywhere, exposed yellowing pipes and a loose mirror about to fall into the sink. The toilets look like the end of the world.

I venture downstairs to the quarters where the family live. The landlady shows me around. We stand in front of Room 9, which is still locked. I ask if it's possible to see inside. She replies that it is a bit messy. I tell her I don't mind, so she comes back with the key and opens the door. Inside is an unmade bed, an old radio and dark wooden wardrobes. A single naked light bulb dangles from the ceiling.

Image copyrightBBC Richard Hamilton

She tells me Burroughs had lived in Room 9, while fellow Beat writers Allen Ginsberg and Jack Kerouac had rented Room 4 and Room 5 on the floor above. Very occasionally, she says, the American novelist Paul Bowles, the author of The Sheltering Sky, would use number 7 at the top. Like Naked Lunch, The Sheltering Sky was another groundbreaking novel that explores the dark side of the human psyche amid the desolate backdrop of the Sahara.

But why were these giants of American literature so attracted to Tangier?

"I think you know the reason," replies Simon-Pierre Hamelin with a smile, when I put this question to him, and says no more. He runs La Librairie des Colonnes, a bookshop on the Boulevard Pasteur, owned by the former boyfriend of Yves Saint Laurent.

Its bookshelves are another reminder of Tangier's huge literary legacy which includes Jean Genet, Andre Gide, Tennessee Williams, Truman Capote, Gore Vidal and Joe Orton, all of whom were gay or bisexual, as well as many others, from Samuel Pepys to Mark Twain, who were straight.

Image copyrightAFPImage caption
Tangier's Boulevard Pasteur, 1944

For decades Tangier and other Moroccan cities were magnets for gay tourists. Prior to independence in 1956 Tangier was an international zone that was administered by several different European countries, without a very rigid rule of law. In the words of the English academic Andrew Hussey, Tangier was "a utopia of dangerous, unknown pleasures." The Americans who turned up in the 1950s were escaping from a repressive society where homosexuality was outlawed. In Morocco, attitudes were much more relaxed and, provided they were discreet, Westerners could indulge their desires, without fear of harassment, with a limitless supply of young locals in need of money, and smoke an equally limitless supply of the local cannabis.

The differential in wealth between foreigners and Moroccans created a thriving market in prostitution, but relations were not only based on the exchange of money. Paul Bowles had a long-lasting friendship with the artist Ahmed Yacoubi, and his wife Jane lived in an apartment upstairs with a wild peasant woman called Cherifa.

In his early days in Tangier, Burroughs was not particularly sensitive to local culture. In a letter to Allen Ginsberg in 1954, he is not even able to keep track of his conquests:

"I go to bed with an Arab in European clothes. Several days later… I meet an Arab in native dress, and we repair to a Turkish bath. Now I am almost (but not quite) sure it is the same Arab. In any case I have not seen no.1 again... It's like I been to bed with 3 Arabs since arrival, but I wonder if it isn't the same character in different clothes, and every time better behaved, cheaper, more respectful… I really don't know for sure."

In his 1972 autobiography Second Son, David Herbert, an English aristocrat and long time resident of Tangier, bemoaned the city's "Queer Tangier" reputation. "There is one aspect of Tangier life that many of us who live here do find disagreeable and occasionally embarrassing." He added that its "old reputation as a city of sin" attracted Europeans who seemed to imagine that "every Moroccan they see is for sale. Great offence is caused by their lack of discrimination and if someone gets knocked on the head it is usually their own fault."

In his diary, the English playwright Joe Orton recorded a conversation at the Cafe de Paris in 1967. Orton was sitting at a table with friends beside a "rather stuffy American tourist and his disapproving wife." To further stoke their disapproval, the playwright began to talk about a sexual encounter. When one of those at the table reminded Orton that the tourists could hear every word, he replied, "they have no right to be occupying chairs reserved for decent sex perverts."

For some straight men the predominance of gay men had its advantages. The septuagenarian American travel writer John Hopkins says: "I was the only heterosexual writer in Tangier at the time. In terms of women, I had the field to myself!"

Image copyrightGetty Images

Although some think the writers were rebelling against a soulless, suburban McCarthyite America, Hopkins says it was more straightforward. "They were after boys and drugs. That's what drew them. The Moroccans were charming, attractive, intelligent and tolerant. They had to put up with a lot from us."

So why did Morocco, an ostensibly devout Islamic country, allow homosexuality to thrive? The author Barnaby Rogerson says it is a society that is full of paradoxes.

"It is... a place where all the four different cornerstones of culture: Berber-African, Mediterranean, Arabic or Islamic, share an absolute belief in the abundant sexuality of all men and women, who are charged with a sort of personal volcano of 'fitna', which threatens family, society and state with sexually derived chaos at any time," he says. The word fitna, he suggests, "means something like 'charm, allure, enchantment, temptation, dissent, unrest, riot, rebellion' or all of these at the same time."

But despite a certain fear of this chaos of sexuality, there is also an understanding that it is just part of human nature and that ultimately you have to live and let live. "Morocco," Rogerson says, "has always been a nation where tolerance is practised but not preached."