A same-sex American-Spanish couple won a high-profile custody battle Tuesday against a surrogate mother in Thailand who gave birth to their child but then decided she wanted to keep the baby when she found out they were gay.

Bangkok's Juvenile and Family Court ruled that the legal guardian of the 15-month-old child, named Carmen, is her American biological father, Gordon Lake, said Lake's lawyer Rachapol Sirikulchit.

'The court has granted legal custody of Carmen Lake to Gordon Lake, my client, and (said) that my client is her only guardian,' Rachapol said.

American Gordon Lake, right, and his Spaniard partner Manuel Santos, left, won full custody of their daughter Carmen (center) on Tuesday, after a legal battle with the girl's Thai surrogate mother. The family pictured above on March 23

The Bangkok court said in a statement that it granted custody to the couple, who live in Spain but have been caring for the 15-month-old in Bangkok, in order to 'protect the well-being of the baby'.

'Based on evidence and witness testimonies the judge was convinced that the girl's custodians took care of her with love,' it said.

'Their homosexuality is not an obstacle to raising the girl and to making her happy like any other child,' the statement added.

Lake and his partner, Spaniard Manuel Santos, both 41, have been stuck in Thailand since launching their legal battle after Carmen was born in January 2015.

Santos emerged from the court smiling and with tears in his eyes.

Santos emerged from court on Tuesday wearing a 'love wins' t-shirt. A Thai judge ruled that his partner was the legal guardian of their daughter Carmen

Carmen is Santos' partner's biological child with an anonymous egg donor. Carmen's surrogate mother, Patidta Kusolsang, has no genetic relation to the child. Above, Santos leaving court on Tuesday

The couple will still have to wait at least 15 months before they are allowed to leave the country with their daughter, if Kusolsang decides to appeal the judge's decision

'We won,' he told reporters. 'We are really happy. ... This nightmare is going to end soon.'

'After 15 months, Carmen will fly to Spain,' Santos said.

Rachapol said the couple would not be able to take Carmen out of the country right away pending the possibility of an appeal by the surrogate mother, Patidta Kusolsang. She was not in court and her intentions could not immediately be learned.

Lake and Santos celebrated their legal victory on the 'Bringcarmenhome' Facebook page set up to support their custody fight.

'There is no way to express with words what we are feeling!' they posted. 'We are crying, our family is crying, our friends are crying, and we are sure all the Thai people who showed their love for us during this time are crying too.'

'Today is a huge day for love, for family and for truth. And it is also a big day for LGBT rights,' said their posting, referring to lesbian, gay, bisexual, and transgender rights.

The couple also have a toddler son named Alvaro, who was born to a surrogate mother in India, with Santos as the biological father. The couple pictured above with their daughter Carmen

Lake, left, and Santos, right, stare lovingly at their daughter in a snap posted to the couple's Instagram page

Lake, left, and Santos, right, pictured above with their daughter and son, shortly after Carmen's birth last year

Lake and Santos will raise Carmen in Spain, but say they will return to her native Thailand often, since they are proud of her heritage

The case was seen as complicated by the fact that Thai law does not recognize same-sex marriages and also by a new law that bans commercial surrogacy, which took effect after Carmen's birth. Rachapol said the court's ruling was based on a transitory clause in the law allowing the intended parents of any baby born before the law took effect to request to be the legal parents.

When Carmen was born, Patidta handed over the baby to Lake and Santos, who left the hospital with the infant. But they say Patidtda then changed her mind and refused to sign the documents to allow Carmen to get a passport so they could leave Thailand.

Lake, who is from New Jersey, is Carmen's biological father, while the egg came from an anonymous donor, not Patidta. Neither he nor Carmen were in court Tuesday.

Lake and Santos were told Patidta had thought they were an 'ordinary family and that she worried for Carmen's upbringing,' according to a message Lake posted on a crowdfunding site that has raised $36,000 to help cover the costs of the trial and staying in Thailand.

Lake has said he doesn't know why the surrogate says she didn't realize they were gay. He says he was clear about that from the start with their surrogacy agency, New Life, which has branches in several countries.

The Bangkok-based New Life office has closed since commercial surrogacy was outlawed in Thailand in July 2015, following several high-profile scandals. There was a grace period provided for parents whose babies were already on the way.

Carmen has lived since birth with the couple, who also have a toddler son, Alvaro, born to a surrogate mother in India with Santos the biological father.

They said in their Facebook posting that the family will live in Valencia, Spain, but that they love Thailand and promised to come back often.