Salim's parents sent him to a psychiatrist. Ali's family beat him and imprisoned him in the house for five days before he fled. Al-Husseins brother threw him down the stairs then shot him in the leg. These are just a few of the reactions when families in the Middle East discover that a son or daughter is gay. Not surprisingly, many gay and lesbian youngsters try to keep their sexuality secret – only to be pressurised into loveless marriages where parents take on the task of finding them a partner.

Homosexuality is a taboo subject in most of the Middle East, even among those who are pressing for political and social reform. If mentioned at all, it's treated as a subject for ribald laughter or (more often) as a foul, unnatural, repulsive, un-Islamic, western perversion. Modern understandings of sexual orientation have not yet taken hold in the Arab countries – with the result that any kind of sexual nonconformity tends to be viewed either as wilfully perverse behaviour or as a symptom of mental illness, and dealt with accordingly.

In the words of IslamOnline, one of the largest Muslim websites, homosexuality is "the most heinous" sin in Islam and "one of the most abominable" – a sin so "enormous in intensity and gravity" that it must be punished both in this life and the next.

Although Muslim websites often have uncertain credentials, IslamOnline claims more authority than most. It assures readers that all its content is checked to ensure that nothing "violates the fixed principles of Islamic law". Its supervisory committee is chaired by Yusuf al-Qaradawi, head of sharia studies at Qatar University and widely regarded as one of the world's foremost Islamic scholars.

Not content merely to disapprove of homosexuality, IslamOnline goes further. Muslims should avoid gay people and must not "associate with them" or "take them as friends", it says. Muslims living in the west also have a religious duty to work with non-Muslims to make the streets 'free' from homosexuality.

According to IslamOnline, homosexuality is a personal 'choice' and the way to correct it is through repentance, prayer, psychiatry and marriage to a person of the opposite sex at the earliest opportunity. These are not, in fact, orthodox Islamic views but they appeal to popular prejudices and reflect a general hardening of attitudes towards homosexuality in the Arab and Muslim world.

Oddly, IslamOnline does not rely on Muslim scripture to support its argument that homosexuality can be 'cured'. Instead, it draws on 'scientific' evidence from conservative Christians and Jews in the United States, and especially the National Association for Research and Therapy of Homosexuality (NARTH), a fringe psychiatric organisation which promotes 'sexual reorientation therapy'.

Statements from mainstream psychiatric bodies in the US and elsewhere that homosexuality is not an illness, and that attempting to 'cure' it can do more harm than good, are dismissed by IslamOnline on the grounds that they are all under the influence – along with most of the western media – of gay activists.

Historically, Arab societies have been relatively relaxed about sexual diversity – perhaps more so than others. Evidence of their previous tolerance can be found in classical Arabic literary works, in the accounts of early travellers and the examples of Europeans who settled in Arab countries to escape sexual persecution at home.

Muslim societies have traditionally recognised that people can be attracted to members of their own sex, and have usually seen nothing sinful in that. It is worth remembering that in the Koranic vision of paradise, along with the famous 72 virgins, the faithful enjoy endless supplies of non-alcoholic drinks served by cute young waiters.

In orthodox Muslim teaching, the question of sin only arises when people act upon their sexual impulses. Same-sex acts are not among the small number of hadd crimes – the most serious crimes specified in the Koran, so what punishment – if any – should be applied to them is a matter of opinion and interpretation.

The general idea in Islam is that sex should take place within a legalised framework, which includes conventional marriage but can also extend to other forms of contractual relationship (such as slavery in the days when that was practised). The point of this is to avoid disputes about parentage and inheritance in the event of pregnancy – an issue that is unlikely to arise in the case of same-sex couples.

Early European travellers in the Middle East were struck by attitudes towards homosexuality that appeared far more relaxed than those commonly found in Europe at the time. Their accounts probably contained a good deal of truth, though many were grossly exaggerated. A 17th century French visitor to the Middle East went so far as to claim that Muslims were bisexual by nature and in 1800, a European traveller to Egypt wrote: "The inconceivable inclination which has dishonoured the Greeks and Persians of antiquity constitutes the delight, or, more properly speaking the infamy of the Egyptians... the contagion has seized the poor as well as the rich."

Sir Richard Burton, the 19th century British orientalist, described in great detail a 'Sotadic Zone' (between the northern latitudes of 30 and 43 degrees) where same-sex activity was thought to be especially prevalent. Presumably it had something to do with the heat.

Today, the tables have been turned and it is the west – in the popular Arab imagination – that has become a region of licentiousness characterised, among other things, by depictions of female nudity and male homosexuality.

In the Arab media, homosexuality is rarely mentioned and tends to be treated mainly as a foreign phenomenon. In the archive of al-Jazeera's website, for example, 18 out of 20 news items mentioning 'gay' or 'lesbian' deal with homosexuality in a non-Arab context, such as western debates about same-sex marriage and gay clergy.

Even when local instances of homosexuality come to light, it is convenient to blame foreign influences – implying that such behaviour could not occur naturally in an Arab-Islamic society. The notorious 'Queen Boat' case in Egypt, when more than 50 allegedly gay men were rounded up and put on trial in 2001, was treated as a matter of national security and the accused were said to have 'imported' their 'perverse ideas' from Europe and, of course, Israel.

Modern Arabic literature also taps into this theme, with tales of same-sex encounters between Arabs and foreigners in which the relationship serves as a metaphor for exploitation and domination by the west. That approach to the subject inevitably rules out the more positive descriptions of same-sex love and sensuality found in Arabic literature of the classical period.

This is one example of how attitudes towards homosexuality in the Middle East have become bound up with international politics. In contrast to the certainties and self-confidence of the classical period, more recent times brought colonialism followed, in the second half of the 20th century, by humiliating wars with Israel and all the challenges of modernity which have had a profound effect on the Arab psyche.

As a defensive mechanism and a re-assertion of lost identity, this has casued a retreat into imagined Arab-Islamic customs and traditions, accompanied by puritanical moral values of a kind once favoured in the west.

Arab portrayals of homosexuality as a foreign phenomenon can also be attributed, at least in part, to a reversal of orientalism. Western orientalism, as analysed by Edward Said, highlights the 'otherness' of oriental culture in order (Said argued) to control it more effectively. Reverse orientalism, as more recently deployed by Arabs, highlights the 'otherness' of the west in order to resist modernisation and reform – and homosexuality is one aspect of western otherness that can be readily harnessed to whip up popular sentiment.

Although it would be foolish to suggest that these attitudes can be easily changed, cultural arguments against homosexuality based on the supposedly unique qualities of Arab society do not bear much scrutiny. The denunciations of sexual non-conformity emanating from the Arab world today are uncannily similar, in both their tone and their arguments, to those that were heard in Britain during the 1950s – including talk of a threat to the national way of life.

None of this has very much to do with religion either, except that religion is often invoked to give legitimacy and respectability to anti-gay prejudices, and the supposedly immutable laws of Islam are cited as a reason why such prejudices should remain unchallenged.

There is a very widespread notion that Islam condemns homosexuality totally and unequivocally – and that is certainly the opinion of the vast majority of Islamic scholars. Much the same could be said of Christianity and Judaism too, however, until just a few decades ago. Since then, significant numbers of Jews and Christians, though not Muslims, have moved towards a more tolerant view.

Why these changes have occurred in Christianity and Judaism but not in Islam is an intriguing question, especially since all three religions are closely related. One important factor has undoubtedly been the spread of secularism. Separation of church from state had a huge impact on Christianity, allowing official doctrine to be questioned and permitting a diversity of views. In the Muslim world, with a few exceptions such as Turkey, secularism has not taken hold.

Changes in Christian attitudes towards homosexuality can also be viewed as part of an historical progression, stimulated by a concern for social justice and respect for all human beings, that possibly has its roots in the anti-slavery campaigns of the late 18th and early 19th centuries, but can certainly be traced back through the battles for racial equality and women's rights in the 20th century. Comparable social pressures in the Muslim world have generally been weak or absent.

The ineffectiveness of feminist movements within Islam is another factor. In Christianity the battles fought by feminists did much to prepare the way for recognition of homosexuality. More generally, at a time when Christian thinkers have been exploring new horizons, Islam has been influenced by the regressive social currents that invoke 'traditional' values as a defence against perceived threats from outside.

Despite all that, there is no obvious reason why Muslims could not adopt a tolerant attitude towards homosexuality if they chose to do so. The theological issues in Islam are remarkably similar to those found in Christianity. They hinge largely on such familiar questions as whether scripture should be taken literally and whether sex is for procreation or pleasure.

As with the Bible, the Koran has a number of verses that are often interpreted as condemning homosexuality. As with the Bible also, they are rare and open to a variety of interpretations. Islamic scholars who denounce homosexuality usually move on very quickly from the Koran to the hadith – sayings attributed to the Prophet Muhammad which in many cases are of dubious authenticity.

The centrepiece of the Islamic case against homosexuality is of course the destruction of Sodom and Gomorrah. The Old Testament version of the story, in which God destroys the cities because of the deviance of their citizens, was dissected by Christian scholars years ago and found to be not what almost everyone had assumed. The Koranic version is more sketchy but its key points are the same – though its popular interpretation has never been seriously challenged.

Instead, highly fanciful embellishments of the tale can be found on Islamic websites. "The Sodomites even indulged in their homosexual orgies publicly," one says. "Their wickedness had attained the level where the mere sight of a handsome young man made them so agitated that they pounced upon him as famished people would fall on foodÂ…." None of that is supported by evidence from the Koran.

In secular western societies debates about religion and sexuality are of real concern only to believers. In Muslim countries it's a different matter. Almost all Arab states, for example, enshrine Islam as the official religion in their constitution and even where the sharia is not applied systematically, the opinions of Islamic scholars cannot be discounted when formulating laws and government policies. Increasingly, Muslim countries also deploy 'Islamic' arguments to block human rights legislation and reverse liberal social policies at an international level.

In many parts of the world nowadays, gay Jews and Christians can find fellow-believers who are supportive rather than judgmental. This is not to belittle the mental anguish that many of them still experience in trying to reconcile faith with their sexuality, but for Muslims it is far more difficult. The most they can hope for, if not to be totally shunned and condemned, is to be told to change their ways and marry, or to spend their lives in celibacy.

It does not help them at all when gay rights campaigners in the west merely parrot the line of conservative scholars that Islam condemns homosexuality. In a region where religion is so pervasive, countering the scholars with secular arguments cuts little ice, as Muslim feminists have begun to discover. To make any headway, the scholars have to be challenged on their own ground – with Islamic arguments for reform.

One consequence of reactionary religious teaching in the Middle East is a host of laws which seek to regulate personal behaviour, whether it is the kind of books people can read, the kind of music they can listen to, or what they can do in bed.

Same-sex acts (even in private, between consenting adults) are illegal in most of the Arab countries, and in other countries more generalised 'immorality' laws can be used to the same effect. Apart from the fundamental issue of whether it is the proper function of governments to legislate on these matters, such laws inevitably bring the legal systems into discredit by encouraging police malpractice (entrapment, forced confessions) and promoting blackmail of gay people.

Although the reactionary voices in the Middle East are the ones most loudly heard, they are beginning to be challenged – especially by the educated urban young, and largely as a result of increased contact with the rest of the world, through satellite television, foreign travel and the Internet.

In Beirut three years ago, 10 openly gay people marched through the streets waving a home-made rainbow flag as part of a protest against the war in Iraq. It was the first time anything like that had happened in an Arab country and their action was reported without hostility by the local press.

Today, Lebanon has an officially-recognised gay and lesbian organisation known as Helem – again, the only one in an Arab country – as well as Barra, the first gay magazine in Arabic.

Helem has even won an award from the Lebanese Ministry of Health for its efforts to prevent the spread of HIV and other sexually-transmitted infections. But, as often happens in the Middle East, one arm of government tends to work at cross-purposes with another. The governor of Beirut recently sought to close Helem down and sent the police on a fishing exercise to check whether it had broken any laws. Apparently it hadn't, but Helem's office was searched and three of its organisers were summoned for questioning.

One of the detectives, who clearly has ambitions to become a second Hercule Poirot, enquired: "Do you have sex in your meetings?"

Brian Whitaker's book, Unspeakable Love: Gay and Lesbian Life in the Middle East, is published by Saqi Books