NEW YORK (AP) -- Sunoco Inc. said Tuesday it's getting out of the refining business.The Philadelphia company, which owns two refineries in Pennsylvania, announced plans to sell those refineries and focus on its pipelines and retail gas stations that provide a steadier cash flow."We have made progress in increasing the efficiency of our refineries over the last several years, but given the unacceptable financial performance of these assets, it is clear that it is in the best interests of shareholders to exit this business," Lynn Elsenhans, Sunoco's chairman and CEO, said in a statement.Sunoco's refineries in Philadelphia and Marcus Hook, Pa., can process a combined 505,000 barrels of oil per day. If it cannot sell its refineries, the company will shut down its main processing units in July 2012.The move is one more step in a major transformation for U.S. refining. Marathon Oil Corp. and ConocoPhillips decided to distance themselves from refining, announcing plans earlier this year to spin off their downstream businesses.Analysts say that the moves reflect a greater distaste for the volatility that comes oil refining. Refineries are only profitable when a company can sell refined petroleum products for a higher price than the crude oil it buys as a feedstock. That's not always the case.Sunoco said the decision to get out of refining will cost the company up to $2.2 billion in the third quarter. It could pay another $500 million over the next 12 months in costs related to contract terminations, employee severance payments and other expenses.Meanwhile, Sunoco said its refineries could generate a $2 billion gain at current market prices.In addition to refineries, Sunoco operates 4,900 gas stations in 24 states. It also runs a pipeline network that transports crude oil and refined fuels throughout the Northeast, Midwest and south central regions of the U.S.Sunoco also announced that it has nearly finished buying back $500 million in shares. It bought 13.1 million shares at an average price of $34.74 per share.Its shares rose 17 cents to $36.17 in morning trading.