A homeless man admitted Wednesday in court that he crushed his friend’s skull with a chunk of concrete two years ago after they slept outside a church in Patchogue.

Thomas Lamartina, 33, pleaded guilty in Riverhead on his birthday to first-degree manslaughter in the June 28, 2014 killing of his friend, Nicholas MacQueen, 65, who was also homeless.

In return for the plea, state Supreme Court Justice William Condon agreed to sentence Lamartina to the maximum of 25 years in prison on Sept. 30.

Defense attorney Craig McElwee of Hauppauge said his client, who had been charged with second-degree murder, had been willing all along to plead guilty to manslaughter, but it wasn’t until Wednesday that Suffolk prosecutors agreed to such a deal.

“He has always said he didn’t intend to kill him,” McElwee said.

The attack happened outside Emanuel Lutheran Church, on East Main Street, where the men hoped to get a free breakfast in the morning.

advertisement | advertise on newsday

“They had both passed out on the steps of the church,” McElwee said. “When he [Lamartina] woke up, he was being improperly touched and temporarily lost his mind.”

Lamartina told a similar story to homicide detectives when he was questioned, saying he was responding to an unwanted sexual advance.

Assistant District Attorney Kathleen Kearon said she didn’t believe such a thing happened. It was more likely, she said, that Lamartina killed MacQueen during a drunken rage, which he sometimes had.

During brief questioning by Kearon, Lamartina said he intended to seriously injure MacQueen when he hit him in the head with the chunk of concrete and ended up killing him.

Kearon said MacQueen was hit five times in the head with the concrete. A parishioner found the body later that morning.

McElwee said he had no way of knowing if his client’s story is true, but acknowledged that drinking had fueled some bad behavior for him in the past.

“His problem is that he’s an over-the-top alcoholic,” McElwee said. “Something happened that triggered that rage.”

McElwee said Lamartina has told him that while he’s in prison he hopes to address both his alcoholism and other behavioral issues. Lamartina has been treated in the past for depression and bipolar disorder.