Story highlights Obama equated legalized discrimination of gays to legalized racism in America

Under Kenyan law, sexual activity between men is illegal and punishable with a maximum imprisonment of 14 years

(CNN) President Barack Obama on Saturday lectured Kenyan President Uhuru Kenyatta about his country's gay rights record.

"When you start treating people differently not because of any harm they are doing to anybody, but because they are different, that's the path whereby freedoms begin to erode," Obama said at a joint press conference with the Kenyan leader in Nairobi. "And bad things happen."

Under Kenyan law, sexual activity between men is illegal and punishable with a maximum imprisonment of 14 years. Many Kenyan leaders had encouraged Obama not to discuss gay rights on his first trip to the country as President.

But Obama equated legalized discrimination of gays to legalized racism in America.

"And when a government gets in a habit of people treating people differently, those habits can spread," Obama continued. "As an African-American, I am painfully aware of what happens when people are treated differently under the law."

JUST WATCHED Obama: Entrepreneurship sparks ingenuity Replay More Videos ... MUST WATCH Obama: Entrepreneurship sparks ingenuity 05:36

Read More