Ayoze’s free kick lifted the Cosmos into a 1-0 lead. (Andy Mead/YCJ Photo)

BROOKLYN, N.Y. — The Cosmos salvaged what could have been a disastrous three-game homestand with a big comeback win Friday night.

The two-time defending North American Soccer League champions struck twice in the second half to register a 4-2 comeback win over 10-man Puerto Rico FC at MCU Park.

Walter Restrepo and substitutes Emmanuel Ledesma and Pablo Vranjicán scored in the second half against Puerto Rico, which played the final nine minutes and five minutes of stoppage time a man down due to a straight red card by Rudy Dawson in the 81st minute.

“They came here to battle us,” Cosmos head coach Giovanni Savarese said. “In the second half we came back, and we were able to regain control of the game.”

Out of a possible nine points during the homestand, the Cosmos accrued four points, which would have been devastating had they not won the game.

The Cosmos (5-2-5, 20 points), who moved into second place, started their 10-day homestand with a 4-2 loss to FC Edmonton June 7. That was followed by a 2-2 draw with North Carolina FC and then Friday night’s result vs. lowly Puerto Rico (1-7-4, 7), which is owned by Knicks forward Carmelo Anthony.

With four games remaining in the spring season, the Cosmos still are alive in the chase for the spring title and an automatic berth in the playoffs. They trail first-place Miami FC by (7-1-3, 24 points), which hosts Jacksonville Armada FC Saturday, by four points.

The game started well for the Cosmos as left back Ayoze converted a 24-yard free kick to the left of a defensive wall and into the lower left corner past goalkeeper Austin Pack for a 1-0 advantage and his team-leading third goal.

A combination of sloppy defensive play and miscommunication in the back left New York in a 2-1 halftime hole.

Jairo Puerto equalized for the visitors in the 17th minute as he beat defender Ryan Richter and volleyed home a right-wing cross from Giuseppe Gentile past goalkeeper Jimmy Maurer for a 1-1 deadlock.

Puerto, who has tallied in four consecutive matches, turned playmaker in the 24th minute as his left-wing feed found Conor Doyle from point-blank range and the midfielder drilled home his second goal this season.

Things went from bad to worse for the Cosmos in the 28th minute when captain and center back Carlos Mendes was forced from the match with an apparent hamstring injury. Three minutes later, David Ochieng, who returned Thursday from international duty with the Kenya national team, replaced Mendes.

“We were not aggressive,” Savarese told MSG Networks at halftime. “Puerto Rico came here to be a little aggressive on everything.”

It worked, at least in the opening 45 minutes.

“Right now they [the Cosmos] need to step it up a little bit,” Savarese said.

Puerto Rico FC assistant coach Jack Stefanowski, a New York University graduate, was ejected from the game in the 60th minute.

Ledesma, who signed for the fall season Monday, knotted things up in the 70th minute. El Salvadoran international Andres Flores lofted a corner kick from the right side that bounced once on the opposite side of the field. Ledesma then volleyed home a shot into the right corner for a 2-2 tie.

Four minutes later, Walter Restrepo’s second goal this season boosted the Cosmos into a 3-2 lead. Instead of sending a left-wing cross as he is known for, Restrepo was the recipient of one by Irvin Herrera as he slid the ball past Pack from five yards.

Vranjicán, signed by the Cosmos June 8, made his debut in the 88th minute as he replaced Herrera. He made his presence felt three minutes into stoppage time as he scored his first goal, an insurance tally, from the right side of the penalty area.