Guns drawn, Iranian intelligence agents rushed into the apartment of a Washington Post reporter and his journalist wife in Tehran.

Threatening to kill Jason Rezaian in front of his wife, Yeganeh, the 20 agents in the July 2014 raid tore through their belongings and rifled through drawers, clothes and valuables for an hour.

But perhaps their most eagerly sought target wasn't exactly inside the house: They forced the couple to hand over the passwords to their email and social media profiles.

That raid demonstrated how much of a threat Iran's theocratic government sees in the internet. It has long sought to strictly control cyberspace and social media - and, thereby, the flow of information to the public.

But the Islamic Republic's relationship with the world wide web is far more complicated than simple repression. Over the past four years, authorities have encouraged wider use of the internet among Iranians, hoping to generate the benefits of a more modern economy. As a result, nearly half the population has in its pockets a tool that the state is struggling to constrain: smartphones, with cameras and internet links that let anyone broadcast to the world.

Those smartphones helped spread the startling burst of protests across Iran that opened 2018. The government succeeded in suffocating the flare-up in part by shutting off key social media and messaging apps, but the lesson was clear: The same oxygen that can resuscitate commerce can also give breath to potential revolt.

Authorities' solution has been to create a so-called “halal net,” Iran's own locally controlled version of the internet aimed at restricting what the public can see.

As Iran approaches the 40th anniversary of the revolution that brought its cleric-led rule to power, how it handles the power of cyberspace will be crucial to its future, determining whether it moves to greater openness or seals itself off from the world.

“The Islamic Republic is not black and white. It shows a myriad of contradictions and its internet policy... is one of the great examples of those contradictions,” said Sanam Vakil, an associate fellow at Chatham House who studies Iran. “The government has taken the internet and effectively used it for its own purposes and also has realised the dangers of it as well.”

Since the 1979 Islamic Revolution, how information spreads across mass media has been tightly controlled.

All television and radio broadcasts within Iran are from state-run stations. Satellite dishes remain ostensibly illegal, though they are plentiful, drawing occasional attacks from bat-wielding government enforcers. Journalists face restrictions in what they can cover and where they can travel across a country of 80 million people that's nearly two-and-a-half times the size of Texas.

The internet helped collapse that distance. During Iran's 2009 protests surrounding the disputed re-election of hard-line President Mahmoud Ahmadinejad, still nascent social media spread word of the events among Iranians and brought videos of the shooting death of 26-year-old Neda Agha Soltan to the world.

Iran's government, overseen by Supreme Leader Ayatollah Ali Khamenei, violently suppressed the demonstrations. The crackdown killed dozens and saw thousands imprisoned, with some tortured by their jailers.

Even before the 2009 protests, Iran blocked access to YouTube. Twitter and Facebook followed amid the unrest, as did many other sites later. Some in Iran began using virtual private networks, or VPNs, which allow users to bypass government censorship.

The chief difference between then and the protests that rocked the country coming into 2018 was the massive proliferation of smartphones. As recently as 2014, only an estimated 2 million Iranians possessed one. Today, estimates suggest Iranians own 48 million.

That explosive growth was spurred by the administration of President Hasan Rouhani, a cleric who is a relative moderate within Iran's system. His officials allowed more mobile phone service providers to offer 3G and 4G internet, suddenly making sharing photos and images possible. Home internet connections became faster. The encrypted messaging platform Telegram spread like wildfire. Over 40 million Iranians are estimated to use it, for everything from benign conversations to commerce and political campaigning.

In the recent unrest, protesters used Telegram's mass-messaging channels to share information and videos across 75 cities and towns where demonstrations erupted. Some showed people openly in the streets shouting, “Death to Khamenei!” It shocked many, especially as such cries could bring a death sentence.

When the government temporarily blocked Telegram as well as Instagram, it helped smother the protests within days. Notably, however, Telegram's silencing quickly brought complaints from businesspeople who use its channels to promote and sell their goods.

Even after the unrest, Rouhani argued it was futile trying to shut off an indispensable tool of modern life.

“If you want cyberspace to be useful to the community, come forward with a solution using it to promote the culture instead of blocking it,” he said, noting that past Iranian government tried to stop people from listening to the radio “but this prevention was useless.”

The danger - and potential - of the internet as a weapon came into focus for Iran when it faced the world's first cyberweapon almost a decade ago.

At the height of tensions between Tehran and the West over its nuclear programme, thousands of centrifuges enriching uranium at Iran's underground Natanz facility suddenly began spinning themselves to death. They had been hit by the Stuxnet computer virus, widely believed to be an American and Israeli creation.

Material leaked by Edward Snowden, the former National Security Agency contractor who exposed US government surveillance programmes in 2013, suggested Iran at the time was the country where American spies collected the most electronic data.

Beginning in 2011, Iran worked to strike back.

Among the most spectacular cyberattacks attributed to Iran is Shamoon, a virus that hit the state-run giant Saudi Arabian Oil Company and Qatari natural gas producer RasGas, deleting hard drives and displaying a picture of a burning American flag on computer screens. Saudi Aramco ultimately shut down its network and destroyed over 30,000 computers. A later iteration of Shamoon in late 2016 caused even more damage.

The US blames Iranian hackers for a denial-of-service attack that overwhelmed six major American banks in 2012. US prosecutors in 2016 accused hackers believed to be backed by Iran of attacking dozens of banks and a small dam near New York City. They also have been suspected of targeting the email and social media accounts of Obama administration officials.

Analysts and security experts believe many of these hackers likely receive backing from Iran's Revolutionary Guard, a powerful paramilitary and economic force in the country answerable only to Khamenei himself.

The Guard employs more direct means as well, like its wresting away of the passwords of Rezaian and his wife, recounted in a lawsuit he filed against the Guard and Iran in US federal court.

Similarly, it seized control of the Facebook and email accounts of Iranian-American dual national Siamak Namazi, who remains detained in Iran along with his octogenarian father Baquer. The Guard then pretended to be Namazi in correspondence with US government officials and others, like New Yorker journalist Robin Wright, tricking them into opening a file that gave the hackers access to their computers.

Cyberespionage is even used in Iran's internal rivalries, with attacks on members of the government, particularly officials in Rouhani's Foreign Ministry, including Zarif, according to a recent report by the Carnegie Endowment for International Peace.

“The targeting of members of government - individuals that have already been vetted by the regime - reflects the importance of cyber-surveillance as a tool of the hard-line security establishment to monitor potential rivals for power,” the report said.

Then Iran moved to target the internet itself.

The idea of Iran setting up its own “halal,” or “permissible,” internet first came in 2011 in the wake of the 2009 protests. It's evolved into what's known as the National Information Network (NIN).

It is essentially a net neutrality supporter's nightmare: The network has some 500 government-approved national websites that stream content far faster than those based abroad, which are intentionally slowed, according to a recent report by the Campaign for Human Rights in Iran. Service providers offer cheaper packages to customers accessing only the NIN websites. Search results also are gamed within the network, allowing the government to censor what users find.

One of the principal designers of the network is the Iran Telecommunications Company, owned by proxies of the Guard.

It resembles in a way China's “Great Firewall,” which blocks access to thousands of websites, from Facebook to Twitter to some news outlets. Chinese internet users also find access to websites outside of the country slower.

“Iran's National Information Network may lack the name cachet of the 'Great Firewall,' but its performance in strangling access to opposition content during the most recent protests proved that Iran is hard on China's heels in terms of controlling the flow of information,” the private US intelligence firm Stratfor wrote in a 17 January analysis.

Firuzeh Mahmoudi, the executive director of the San Francisco-based group United for Iran, said authorities have had success in getting businesses to operate on the NIN. The more they do so, he warned, “the easier it will be for them to shut down or throttle the real internet when they want to.”

Hard-liners have suggested removing Iran entirely from the internet and creating its own at home.

“Cyberspace was the kindling in the fire of the battle,” hard-line cleric Ayatollah Ahmad Khatami recently told worshippers at Friday prayers in Tehran. “When cyberspace was closed down, the sedition was stopped. The nation does not support a social network that has its key in the hands of the United States.”

Amid the protests, the Trump administration said it wanted to help Iranians access the internet. The US Treasury sanctioned the Guard, Iran's Supreme Council of Cyberspace and other officials for “engaging in censorship.”

Top Trump administration officials have met with Google, Facebook, Twitter and other leading tech companies to ask what more they can do to help people in Iran and other authoritarian-run countries communicate freely, according to US officials briefed on the meetings.

But fear of crossing US sanctions has made companies skittish. Some firms don't allow their services to be used in Iran. That prevents Iranians from accessing many encrypted communication apps or VPNs.

World news in pictures Show all 50 1 /50 World news in pictures World news in pictures 14 September 2020 Japan's Prime Minister Shinzo Abe, Chief Cabinet Secretary Yoshihide Suga, former Defense Minister Shigeru Ishiba and former Foreign Minister Fumio Kishida celebrate after Suga was elected as new head of the ruling party at the Liberal Democratic Party's leadership election in Tokyo Reuters World news in pictures 13 September 2020 A man stands behind a burning barricade during the fifth straight day of protests against police brutality in Bogota AFP via Getty World news in pictures 12 September 2020 Police officers block and detain protesters during an opposition rally to protest the official presidential election results in Minsk, Belarus. Daily protests calling for the authoritarian president's resignation are now in their second month AP World news in pictures 11 September 2020 Members of 'Omnium Cultural' celebrate the 20th 'Festa per la llibertat' ('Fiesta for the freedom') to mark the Day of Catalonia in Barcelona. Omnion Cultural fights for the independence of Catalonia EPA World news in pictures 10 September 2020 The Moria refugee camp, two days after Greece's biggest migrant camp, was destroyed by fire. Thousands of asylum seekers on the island of Lesbos are now homeless AFP via Getty World news in pictures 9 September 2020 Pope Francis takes off his face mask as he arrives by car to hold a limited public audience at the San Damaso courtyard in The Vatican AFP via Getty World news in pictures 8 September 2020 A home is engulfed in flames during the "Creek Fire" in the Tollhouse area of California AFP via Getty World news in pictures 7 September 2020 A couple take photos along a sea wall of the waves brought by Typhoon Haishen in the eastern port city of Sokcho AFP via Getty World news in pictures 6 September 2020 Novak Djokovic and a tournament official tends to a linesperson who was struck with a ball by Djokovic during his match against Pablo Carreno Busta at the US Open USA Today Sports/Reuters World news in pictures 5 September 2020 Protesters confront police at the Shrine of Remembrance in Melbourne, Australia, during an anti-lockdown rally AFP via Getty World news in pictures 4 September 2020 A woman looks on from a rooftop as rescue workers dig through the rubble of a damaged building in Beirut. A search began for possible survivors after a scanner detected a pulse one month after the mega-blast at the adjacent port AFP via Getty World news in pictures 3 September 2020 A full moon next to the Virgen del Panecillo statue in Quito, Ecuador EPA World news in pictures 2 September 2020 A Palestinian woman reacts as Israeli forces demolish her animal shed near Hebron in the Israeli-occupied West Bank Reuters World news in pictures 1 September 2020 Students protest against presidential elections results in Minsk TUT.BY/AFP via Getty World news in pictures 31 August 2020 The pack rides during the 3rd stage of the Tour de France between Nice and Sisteron AFP via Getty World news in pictures 30 August 2020 Law enforcement officers block a street during a rally of opposition supporters protesting against presidential election results in Minsk, Belarus Reuters World news in pictures 29 August 2020 A woman holding a placard reading "Stop Censorship - Yes to the Freedom of Expression" shouts in a megaphone during a protest against the mandatory wearing of face masks in Paris. Masks, which were already compulsory on public transport, in enclosed public spaces, and outdoors in Paris in certain high-congestion areas around tourist sites, were made mandatory outdoors citywide on August 28 to fight the rising coronavirus infections AFP via Getty World news in pictures 28 August 2020 Japanese Prime Minister Shinzo Abe bows to the national flag at the start of a press conference at the prime minister official residence in Tokyo. Abe announced he will resign over health problems, in a bombshell development that kicks off a leadership contest in the world's third-largest economy AFP via Getty World news in pictures 27 August 2020 Residents take cover behind a tree trunk from rubber bullets fired by South African Police Service (SAPS) in Eldorado Park, near Johannesburg, during a protest by community members after a 16-year old boy was reported dead AFP via Getty World news in pictures 26 August 2020 People scatter rose petals on a statue of Mother Teresa marking her 110th birth anniversary in Ahmedabad AFP via Getty World news in pictures 25 August 2020 An aerial view shows beach-goers standing on salt formations in the Dead Sea near Ein Bokeq, Israel Reuters World news in pictures 24 August 2020 Health workers use a fingertip pulse oximeter and check the body temperature of a fisherwoman inside the Dharavi slum during a door-to-door Covid-19 coronavirus screening in Mumbai AFP via Getty World news in pictures 23 August 2020 People carry an idol of the Hindu god Ganesh, the deity of prosperity, to immerse it off the coast of the Arabian sea during the Ganesh Chaturthi festival in Mumbai, India Reuters World news in pictures 22 August 2020 Firefighters watch as flames from the LNU Lightning Complex fires approach a home in Napa County, California AP World news in pictures 21 August 2020 Members of the Israeli security forces arrest a Palestinian demonstrator during a rally to protest against Israel's plan to annex parts of the occupied West Bank AFP via Getty World news in pictures 20 August 2020 A man pushes his bicycle through a deserted road after prohibitory orders were imposed by district officials for a week to contain the spread of the Covid-19 in Kathmandu AFP via Getty World news in pictures 19 August 2020 A car burns while parked at a residence in Vacaville, California. Dozens of fires are burning out of control throughout Northern California as fire resources are spread thin AFP via Getty World news in pictures 18 August 2020 Students use their mobile phones as flashlights at an anti-government rally at Mahidol University in Nakhon Pathom. Thailand has seen near-daily protests in recent weeks by students demanding the resignation of Prime Minister Prayut Chan-O-Cha AFP via Getty World news in pictures 17 August 2020 Members of the Kayapo tribe block the BR163 highway during a protest outside Novo Progresso in Para state, Brazil. Indigenous protesters blocked a major transamazonian highway to protest against the lack of governmental support during the COVID-19 novel coronavirus pandemic and illegal deforestation in and around their territories AFP via Getty World news in pictures 16 August 2020 Lightning forks over the San Francisco-Oakland Bay Bridge as a storm passes over Oakland AP World news in pictures 15 August 2020 Belarus opposition supporters gather near the Pushkinskaya metro station where Alexander Taraikovsky, a 34-year-old protester died on August 10, during their protest rally in central Minsk AFP via Getty World news in pictures 14 August 2020 AlphaTauri's driver Daniil Kvyat takes part in the second practice session at the Circuit de Catalunya in Montmelo near Barcelona ahead of the Spanish F1 Grand Prix AFP via Getty World news in pictures 13 August 2020 Soldiers of the Brazilian Armed Forces during a disinfection of the Christ The Redeemer statue at the Corcovado mountain prior to the opening of the touristic attraction in Rio AFP via Getty World news in pictures 12 August 2020 Young elephant bulls tussle playfully on World Elephant Day at the Amboseli National Park in Kenya AFP via Getty World news in pictures 11 August 2020 French Prime Minister Jean Castex is helped by a member of staff to put a protective suit on prior to his visit at the CHU hospital in Montpellier AFP via Getty World news in pictures 10 August 2020 Locals harvest their potatoes as Mount Sinabung spews volcanic ash in Karo, North Sumatra province, Indonesia Antara Foto/Reuters World news in pictures 9 August 2020 Doves fly over the Peace Statue at Nagasaki Peace Park during the memorial ceremony held for the 75th anniversary of the atomic bombing EPA World news in pictures 8 August 2020 Anti-government protesters try to remove concrete wall that installed by security forces to prevent protesters reaching the Parliament square, during a protest against the political elites and the government after this week's deadly explosion in Beirut AP World news in pictures 7 August 2020 A protester throws a stone towards Israeli forces in the village of Turmus Aya, north of Ramallah in the occupied West Bank, following a march by Palestinians against the building of Israeli settlements AFP via Getty World news in pictures 6 August 2020 A woman yells as soldiers block a road for French President Emmanuel Macron's visit the Gemmayzeh neighborhood. The area in Beirut suffered extensive damage from the explosion at the seaport AP World news in pictures 5 August 2020 Damage at the site of Tuesday's blast in Beirut's port area, Lebanon Reuters World news in pictures 4 August 2020 A large explosion in the Lebanese capital Beirut. The blast, which rattled entire buildings and broke glass, was felt in several parts of the city AFP via Getty World news in pictures 3 August 2020 A general view shows the new road bridge in Genoa, Italy ahead of its official inauguration, after it was rebuilt following its collapse on August 14, 2018 which killed 43 people Reuters World news in pictures 2 August 2020 Empty stall spaces are seen hours before a citywide curfew is introduced in Melbourne, Australia EPA World news in pictures 1 August 2020 People take part in a demonstration by the initiative "Querdenken-711" with the slogan "the end of the pandemic - the day of freedom" to protest against the current measurements to curb the spread of COVID-19 in Berlin, Germany AFP via Getty World news in pictures 31 July 2020 Pilgrims circumambulating around the Kaaba, the holiest shrine in the Grand mosque in Mecca. Muslim pilgrims converged today on Saudi Arabia's Mount Arafat for the climax of this year's hajj, the smallest in modern times and a sharp contrast to the massive crowds of previous years Saudi Ministry of Media/AFP World news in pictures 30 July 2020 The Mars 2020 Perseverance mission lifts off at the Kennedy Space Centre in Florida. The mission is part of the USA's largest moon to Mars exploration. Nasa will attempt to establish a sustained human presence on and around the moon by 2028 through their Artemis programme EPA World news in pictures 29 July 2020 A woman refreshes herself in a outdoor pool in summer temperatures in Ehingen, Germany dpa via AP World news in pictures 28 July 2020 Malaysia's former prime minister Najib Razak speaks to the media after he was found guilty in his corruption trial in Kuala Lumpur AFP via Getty World news in pictures 27 July 2020 North Korean leader Kim Jong Un poses for a photograph after conferring commemorative pistols to leading commanding officers of the armed forces on the 67th anniversary of the "Day of Victory in the Great Fatherland Liberation War". Which marks the signing of the Korean War armistice KCNA via Reuters

Even when the Trump administration has floated the possibility of easing some sanctions or offering carve-outs, some tech companies have been reluctant to offer more services in Iran, said the officials, who weren't authorised to discuss the conversations and demanded anonymity.

So it remains in question whether Iranians will have access to an open internet if anger over the economy boils over into protests again, as many predict it will.

“We believe that the US government could do more to enable the free flow of information in Iran and establish a thriving entrepreneurial civil society independent of the regime,” said Morad Ghorban of the Washington-based Public Affairs Alliance of Iranian Americans. “This movement has continued despite persecution by hard-line elements.”