As the seventh inning started at Fenway Park Friday night, the Red Sox were nine outs from what would have been their signature victory of the season.

Down by five runs in the first inning against the Yankees, the determined Sox overcame a terrible start by Josh Beckett to take a one-run lead. Manager Bobby Valentine also had his best relievers lined up just the way he wanted them.

“We thought we could squeak it out,” Valentine said.

But perseverance could not overcome another round of poor pitching. The Yankees scored four runs in the seventh inning and beat the Sox, 10-8, in a wild game played before 38,066.

That’s four losses in a row for the Red Sox, who trail the first-place Yankees by 8½ games in the division. It’s their largest deficit of the season.

“It’s only July,” Adrian Gonzalez said, dismissing the idea that the Red Sox are falling out of the race.

But the Sox are winless in three games against the Yankees this season, giving up 31 runs. With three more games this weekend, the rivalry is all in New York’s favor so far.

“We showed a lot of character coming back,” shortstop Mike Aviles said. “But we needed to find a way to win it.”

The first inning took 42 minutes as 68 pitches were thrown. By the time it was over, each team scored five runs and everyone at Fenway Park was buckling in for a long night of baseball.

The first five Yankees reached off Beckett, who nearly didn’t finish the inning.

“I was really struggling. In the first inning I just wanted to throw a strike. I didn’t care where they hit it. I was really battling myself,” he said.

Derek Jeter flicked the first pitch of the game into center field for a single. Curtis Granderson singled to right before a curveball brushed Alex Rodriguez to load the bases.

Beckett walked Robinson Cano to force in a run. Mark Teixeira then singled to center to drive in two more. A sacrifice fly by Nick Swisher gave the Yankees a 4-0 lead.

Teixeira took third on a single by Raul Ibanez and scored on a sacrifice fly by Eric Chavez.

The Red Sox roared back against Hiroki Kuroda, also sending nine men to the plate.

Daniel Nava led off with a double off the wall, advanced to third on a wild pitch, and scored on a sacrifice fly by Ryan Kalish.

David Ortiz singled to left, beating the shift. After Cody Ross reached on an error, Gonzalez doubled off the wall in left-center to drive in Ortiz and extend his hit streak to 16 games.