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.

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.

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

"After 15 months, Carmen will fly to Spain," where the couple lives, 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 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 Patidta 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.

"Carmen is half Thai and we are very very proud of that," they said. "Right now, we just want to go back to our normal lives and try to rebuild what we can, so that Alvaro and Carmen can have the wonderful lives that they were always meant to have."