The High Court has told the NHS in England it can fund a drug that can prevent HIV - after health bosses argued it was not their responsibility.
NHS England previously said councils should provide the pre-exposure prophylaxis (Prep) drug as they are in charge of preventative health.
This stance was successfully challenged by the National Aids Trust (NAT).
But the High Court ruling does not make funding of Prep automatic and the NHS is set to appeal.
The ruling by Mr Justice Green said health bosses had "erred" in arguing it was not their responsibility.
NHS England has already announced it will appeal against the ruling - and even if that goes against health bosses it is not a given that Prep will be considered effective enough to warrant NHS funding.
If the Court of Appeal uphold the ruling NHS bosses would then assess Prep's cost-effectiveness alongside the merits of other treatments the NHS is being asked to provide.
Using Prep has been shown to reduce the risk of HIV infection by 86%.
The once-a-day pill, which costs £400 a month per person, works by disabling the virus to stop it multiplying.
It is currently used in the US, Canada, Australia and France to help protect the most at-risk gay men.
'This is about saving lives'
Harry Dodd, 25, is one of about 500 homosexual men in England who are taking Prep as part of a trial called Proud.
He says: "I've seen the panic on the face of previous boyfriends when they are awaiting their [HIV test] results - it's a huge fear and it affects everything you do.
"To be able to have sex without having that fear hanging over you all the time is huge."
Harry says taking Prep has still not become socially acceptable.
"Too many people seem to think it will encourage a hedonistic lifestyle, but for me this is about saving lives," he says.
"People reacted with cynicism when the contraceptive pill for women was first introduced.
"For me, taking Prep has helped me to trust again, have relationships and build bridges and that shouldn't be taken away."
NHS England had argued that because Prep was preventative it was not its responsibility.
In May, it said it had legal advice that said it did not have the "legal power to commission Prep" and that under 2013 regulations "local authorities are the responsible commissioner for HIV prevention services".
NHS England has also warned that if it prioritised Prep, there was a risk of a legal challenge from people wanting similar access to other preventative treatments.
But the National Aids Trust (NAT) said local authorities did not have sole responsibility for HIV prevention in England.
The NHS in Wales, Scotland and Northern Ireland have not yet made a decision on Prep.
Deborah Gold, chief executive of NAT, said: "This is fantastic news. It is vindication for the many people who were let down when NHS England absolved itself of responsibility for Prep."
She urged NHS to act immediately and start funding Prep.
"Prep works. It saves money and it will make an enormous difference to the lives of men and women across the country who are at risk of acquiring HIV. The delay to commissioning Prep is both unethical and expensive."
But a spokesman for NHS England said an appeal would be launched first.
"NHS England has considered the judgement carefully and has taken legal advice. Queen's Counsel has advised that the court's ruling interprets the legislation governing NHS England's role and functions in a way that is inconsistent with Parliament's intention."
Meanwhile Dr Jonathan Fielden of NHS England, told the BBC: "Prep, subject to the appeal, will seen and considered alongside 13 other treatments including treatments for children with cystic fibrosis, for prosthetic limbs and certain types of auditory implants for deafness."
Councillor Izzi Seccombe, of the Local Government Association, which represents councils, said she was "pleased" with the ruling.
"We firmly rejected the argument by the NHS that it should fall to councils."